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. Android 使用Retrofit获取JSON数据

    在大家使用网络请求的时候,往往会出现一种情况:需要拿到服务器返回来的JSON字符串,而Retrofit会默认将Json解析,而又没有直接暴露出拿到Json字符串的方法: 今天测接口的时候,发现当数据正 ...

  2. C#中如何获得两个日期之间的天数差

    DateTime d1; DateTime d2; //自己去赋值吧 int days = (d1 - d2).Days;//天数差 label1.Text = "2012-1-1 15:3 ...

  3. jquery.base64.js

    (function($) { $.base64 = function(options) { var defaults = { data:"", type:0, unicode:tr ...

  4. “==”和Equals区别

    相信很多朋友在面对,对象判等时经常会犹豫是用“==”还是Equals呢?有时候发现两者得到的结果相同,但有时候有不同, 究竟在什么情况下"==" 会相等,什么情况下Equals会不 ...

  5. weex入门(一)

    emmmm其实没有接触过weex ,了解一番发现有很多坑,有很多基于weex改良后的框架,比如weexplus等等,基本不用踩多少坑.经过几天的深思熟虑我觉得还是去踩坑,毕竟踩完坑才能真正的了解嘛 w ...

  6. 昼猫笔记--什么是DOM

    昼猫笔记--给你带来不一样的笔记 不止是笔记 更多的是思考 Hello,大家好,昼猫,今天来加深下DOM 什么DOM呢?它的全称叫 Document Object Model 通过全称可以知道它是 文 ...

  7. PatentTips - System and method to deprivilege components of a virtual machine monitor

    BACKGROUND INFORMATION An embodiment of the present invention relates generally to virtualization pl ...

  8. HDU 4588 Count The Carries 数位DP || 打表找规律

    2013年南京邀请赛的铜牌题...做的非常是伤心.另外有两个不太好想到的地方.. ..a 能够等于零,另外a到b的累加和比較大.大约在2^70左右. 首先说一下解题思路. 首先统计出每一位的1的个数, ...

  9. Ubuntu中的解压缩文件的方式

    记录Ubuntu下各种压缩和解压方式: .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) -- ...

  10. 蓝的成长记——追逐DBA(10):飞刀防身,熟络而非专长:摆弄中间件Websphere

    原创作品,出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...