xtu字符串 C. Marlon's String

C. Marlon‘s String

Time Limit: 2000ms

Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

Long long ago, there was a coder named Marlon. One day he picked two string on the street. A problem suddenly crash his brain...

Let Si..j denote the i-th character to the j-th character of string S.

Given two strings S and T. Return the amount of tetrad (a,b,c,d) which satisfy Sa..b + Sc..d = T , ab and cd.

The operator + means concate the two strings into one.

Input

The first line of the data is an integer Tc. Following Tc test cases, each contains two line. The first line is S. The second line is T. The length of S and T are both in range [1,100000]. There are only letters in string S and T.

Output

For each test cases, output a line for the result.

Sample Input

1
aaabbb
ab

Sample Output

9

解题:与扩展KMP无关,与前缀有关。。。直接KMP

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 int fail[100010];
16 char pa[100010],s[100010];
17 int a[100010],b[100010];
18 void kmp(int *arr){
19     int i,j,k;
20     fail[0] = fail[1] = 0;
21     for(i = 1; pa[i]; i++){
22         j = fail[i];
23         while(j && pa[i] != pa[j]) j = fail[j];
24         fail[i+1] = pa[j] == pa[i] ? j+1:0;
25     }
26     for(j = i = 0; s[i]; i++){
27         while(j && s[i] != pa[j]) j = fail[j];
28         if(pa[j] == s[i]){
29             arr[++j]++;
30         }
31     }
32     for(i = strlen(pa); i >= 0; i--)
33         if(fail[i]) arr[fail[i]] += arr[i];
34 }
35 int main(){
36     int t,i,len;
37     LL ans;
38     scanf("%d",&t);
39     while(t--){
40         scanf("%s %s",s,pa);
41         memset(a,0,sizeof(a));
42         memset(b,0,sizeof(b));
43         kmp(a);
44         reverse(s,s+strlen(s));
45         reverse(pa,pa+strlen(pa));
46         kmp(b);
47         ans = 0;
48         for(len = strlen(pa),i = 0; i < len; i++)
49             ans += (LL)a[i]*b[len-i];
50         printf("%lld\n",ans);
51     }
52     return 0;
53 }

xtu字符串 C. Marlon's String

时间: 2024-10-01 03:04:12

xtu字符串 C. Marlon's String的相关文章

ZOJ 3587 Marlon&#39;s String 扩展KMP

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T,S,T<=100000.拿出S的两个子串(可以重叠),将两个子串连接起来成为字符串T的方法有多少种. 思路:用扩展KMP求出S的从每位开始的子串与T的公共前缀,再将两个子串翻转,再用扩展KMP求出S反的从每位开始的子串与T反的公共前缀.找出其中和为T子串长度的S公共前缀和S反的公共前缀的数量,相乘为结果. 代码: #include

xtu字符串 A. Babelfish

A. Babelfish Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you

xtu字符串 B. Power Strings

B. Power Strings Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = &

xtu字符串 D. 病毒侵袭

D. 病毒侵袭 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class name: Main 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒.小t不幸成为受害者之一.小t如此

C#之CLR内存字符串常量池(string)

C#之CLR内存字符串常量池(string) 投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-08-04我要评论 这篇文章主要介绍了C#之CLR内存字符串常量池(string),对于学习和理解C#内存原理很有帮助,需要的朋友可以参考下 C#中的string是比特殊的类,说引用类型,但不存在堆里面,而且String str=new String("HelloWorld")这样的重装也说没有的. 我们先来看一个方法: ? 1 2 3 4 5 6 7 8 cl

SAP ABAP编程 在string类型A字符串中查询匹配string类型B字符串

如题,在string类型A字符串中查询匹配string类型B字符串,方法如下: DATA: a TYPE string VALUE 'ABCDEFGHIJKLNM', b TYPE string VALUE 'EFGHIJ', c TYPE string . DATA: off  TYPE i VALUE 0,   "从自己个字符开始查找 moff TYPE i, mlen TYPE i. FIND b IN SECTION OFFSET off OF a MATCH OFFSET moff &

Java 带分隔字符串、字符串数组和 ArrayList&lt;String&gt; 之间的转换

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 一.先来认识一下标题说的这三件东西,也许描述的不清楚,但有了下面的例子,就不会有歧义了 1.带分隔字符串是这样的: String seperate

斐波拉切字符串统计个数 Fibonacci String

Problem:  s0 = "a", s1 = "b", s2 = "ba", s3 = "bab", s4 = "babba", s4 = "babbabab", is called Fibonacci string. For the string with index n, given a string str = "bb", calculate how man

将格式化字符串赋给stl::string

代码很简单,就不解释了,直接上代码: #include <cstdio> #include <cstdarg> #include <iostream> using namespace std; //功能:将格式化字符串赋给stl::string //参数:和printf的参数一样 //返回值:返回格式化后的string类 string StringFormat(const char *format, ...) { string result; //将字符串的长度初始化为