Marlon's String


Time Limit: 2 Seconds     
Memory Limit: 65536 KB


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

题目大意:Return the amount of tetrad (a,b,c,d) which satisfy
Sa..b + Sc..d = T , ab and
cd.

ac代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[200010],b[200010];
int c1[200010],c2[200010];
int next[200010];
void getnext(char *a)
{
next[0]=next[1]=0;
int i;
int m=strlen(a);
for(i=1;i<m;i++)
{
int j=next[i];
while(j&&a[i]!=a[j])
j=next[j];
next[i+1]=(a[i]==a[j])?j+1:0;
}
}
void kmp(char *a,char *b,int *c)
{
getnext(b);
int j=0,i;
int n=strlen(a);
int m=strlen(b);
for(i=0;i<n;i++)
{
while(j&&a[i]!=b[j])
j=next[j];
if(a[i]==b[j])
{
j++;
c[j]++;
}
}
for(i=m;i>=0;i--)
if(next[i])
c[next[i]]+=c[i];
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i;
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
scanf("%s%s",a,b);
int len1=strlen(a);
int len2=strlen(b);
kmp(a,b,c1);
reverse(a,a+len1);
reverse(b,b+len2);
kmp(a,b,c2);
long long ans=0;
for(i=0;i<len2;i++)
ans+=(long long)c1[i]*c2[len2-i];
printf("%lld\n",ans);
}
}

ZOJ 题目3587 Marlon&#39;s String(KMP)的更多相关文章

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

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T.S,T<=100000.拿出 ...

  2. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  3. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  4. 翻转子串(string+KMP+程序猿面试金典)

    翻转子串 參与人数:1197时间限制:3秒空间限制:32768K 通过比例:35.03% 最佳记录:0 ms|8552K(来自 ) 题目描写叙述 假定我们都知道很高效的算法来检查一个单词是否为其它字符 ...

  5. hdu 3336 Count the string KMP+DP优化

    Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...

  6. hdoj 5311 Hidden String(KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5311 思路分析:该问题要求在字符串中是否存在三个不相交的子串s[l1..r1], s[l2..r2], ...

  7. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  8. 【XSY2472】string KMP 期望DP

    题目大意 给定一个由且仅由字符'H','T'构成的字符串\(S\). ​ 给定一个最初为空的字符串\(T\) ,每次随机地在\(T\)的末尾添加'H'或者'T'. 问当\(S\)为\(T\)的后缀时, ...

  9. Count the string kmp

    问题描述众所周知,aekdycoin擅长字符串问题和数论问题.当给定一个字符串s时,我们可以写下该字符串的所有非空前缀.例如:S:“ABAB”前缀是:“A”.“AB”.“ABA”.“ABAB”对于每个 ...

随机推荐

  1. orm 通用方法——QueryModelById 主键查询

    方法定义: /** * 描述:根据主键查询 * 作者:Tianqi * 日期:2014-09-15 * param:model 对象实例,包含主键 * return:对象 * */ func Quer ...

  2. java.util.logging.FileHandler

    java.util.logging.FileHandler java自带的日志功能,FileHandler可以写日志到文件系统,并且自己维护日志的增删,比c++不知道强多少 FileHandler(f ...

  3. Codeforces Round #240 (Div. 2) 题解

    A: 1分钟题,往后扫一遍 int a[MAXN]; int vis[MAXN]; int main(){ int n,m; cin>>n>>m; MEM(vis,); ; i ...

  4. myeclipse中断点调试

    在代码最左端,也就是行号位置处双击.会出现一个实心小圆点.即增加的断点.debug启动程序,就会运行到断点处: 按F5是进去方法里面. 按F6是一步一步走, 按F7是跳出方法里面(按F5后再按F7就跳 ...

  5. RvmTranslator6.3 is released

    RvmTranslator6.3 is released eryar@163.com RvmTranslator can translate the RVM file exported by AVEV ...

  6. 一些安全的DNS提供商

    检测当前使用的DNS服务器    https://dnsleaktest.com 一些安全的DNS提供商 DNS提供商名称 主DNS服务器 次要DNS服务器 Google 8.8.8.8 8.8.4. ...

  7. 认知 Git 和 GitHub

    今天被一个大牛的大哥问了个问题,问住了.原问题是“你是到github吗?git呢?” 我堂而皇之的说,“他们不是同一个吗?” 结果大牛大哥狠狠地回了我一句“百度去”..好吧我乖乖去百度了. 这是我百度 ...

  8. Kinect 开发 —— 深度信息(二)

    转自(并致谢):http://www.cnblogs.com/yangecnu/archive/2012/04/05/KinectSDK_Depth_Image_Processing_Part2.ht ...

  9. Kinect 开发 —— Hello,Kinect

    控制台输出深度数据: using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  10. HDU 4704 Sum 超大数幂取模

    很容易得出答案就是2^(n-1) 但是N暴大,所以不可以直接用幂取模,因为除法操作至少O(len)了,总时间会达到O(len*log(N)) 显然爆的一塌糊涂 套用FZU1759的模板+顺手写一个大数 ...