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. 1
  2. aaabbb
  3. ab

Sample Output

  1. 9

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

ac代码

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. char a[200010],b[200010];
  6. int c1[200010],c2[200010];
  7. int next[200010];
  8. void getnext(char *a)
  9. {
  10. next[0]=next[1]=0;
  11. int i;
  12. int m=strlen(a);
  13. for(i=1;i<m;i++)
  14. {
  15. int j=next[i];
  16. while(j&&a[i]!=a[j])
  17. j=next[j];
  18. next[i+1]=(a[i]==a[j])?j+1:0;
  19. }
  20. }
  21. void kmp(char *a,char *b,int *c)
  22. {
  23. getnext(b);
  24. int j=0,i;
  25. int n=strlen(a);
  26. int m=strlen(b);
  27. for(i=0;i<n;i++)
  28. {
  29. while(j&&a[i]!=b[j])
  30. j=next[j];
  31. if(a[i]==b[j])
  32. {
  33. j++;
  34. c[j]++;
  35. }
  36. }
  37. for(i=m;i>=0;i--)
  38. if(next[i])
  39. c[next[i]]+=c[i];
  40. }
  41. int main()
  42. {
  43. int t;
  44. scanf("%d",&t);
  45. while(t--)
  46. {
  47. int i;
  48. memset(c1,0,sizeof(c1));
  49. memset(c2,0,sizeof(c2));
  50. scanf("%s%s",a,b);
  51. int len1=strlen(a);
  52. int len2=strlen(b);
  53. kmp(a,b,c1);
  54. reverse(a,a+len1);
  55. reverse(b,b+len2);
  56. kmp(a,b,c2);
  57. long long ans=0;
  58. for(i=0;i<len2;i++)
  59. ans+=(long long)c1[i]*c2[len2-i];
  60. printf("%lld\n",ans);
  61. }
  62. }

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. hosts 文件与 ipv6

    ipv6 的项目地址:ipv6-hosts 正如文档中所说,用于在大陆地区加快 Google.YouTube.Facebook.Wikipedia 等的访问:(twitter 不支持) 使用说明(wi ...

  2. spring之DelegatingFilterProxy

    DelegatingFilterProxy是一个标准servlet Filter的代理,代理实现了Filter接口的spring管理的Bean.支持一个在web.xml的init-param定义的&q ...

  3. display 表格模型值

    display:inline-table 此元素会作为内联表格来显示(类似 <table>),表格前后没有换行符.display:inline-table 可以使文字和表格在同一行显示 d ...

  4. window下搭建Vue.Js开发环境

    一.安装node.js.https://nodejs.org/en/download/ 最新包会自动安装npm 二.安装完node之后,npm包含的很多依赖包是部署在国外的,在天朝,大家都知道下载速度 ...

  5. comm---两个文件之间的比较

    comm命令可以用于两个文件之间的比较,它有一些选项可以用来调整输出,以便执行交集.求差.以及差集操作. 交集:打印出两个文件所共有的行. 求差:打印出指定文件所包含的且不相同的行. 差集:打印出包含 ...

  6. 五十个UI设计资源网站

    五十个UI设计资源网站 用户体验团队网站 1.UCD大社区 http://ucdchina.com/ 2.腾讯WSD http://wsd.tencent.com/ 3.腾讯CDC http://cd ...

  7. Peer To Peer——对等网络

    今年的考试.大问题没怎么出现. 就是考英语第二天的下午,发生网络阻塞的现象,不影响大局.可是事出有因,我们还是须要看看是什么影响到了考生抽题.最后查了一圈,发现其它几场的英语考试听力都是19M大小,而 ...

  8. 43.安装npm及cnpm(Windows)

    转自:https://www.cnblogs.com/yominhi/p/7039795.html [工具官网] Node.js : http://nodejs.cn/ 淘宝NPM: https:// ...

  9. 在kafka/config/目录下面有3个配置文件参数说明(producer.properties。consumer.properties。server.properties)

    (1).producer.properties:生产端的配置文件 #指定kafka节点列表,用于获取metadata,不必全部指定 #需要kafka的服务器地址,来获取每一个topic的分片数等元数据 ...

  10. Impala管理

    这里, 以后更新. Impala的安装(含使用CM安装 和 手动安装)(图文详解) 可以通过下面的链接来访问Impala的监护管理页面: • 查看StateStore – http://node1:2 ...