A Secret

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 612    Accepted Submission(s): 237

Problem Description
Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,which have a big secret.SF is interested in this secret and ask VS how to get it.There are the things that VS tell:
  Suffix(S2,i) = S2[i...len].Ni is the times that Suffix(S2,i) occurs in S1 and Li is the length of Suffix(S2,i).Then the secret is the sum of the product of Ni and Li.
  Now SF wants you to help him find the secret.The answer may be very large, so the answer should mod 1000000007.
 
Input
Input contains multiple cases.
  The first line contains an integer T,the number of cases.Then following T cases.
  Each test case contains two lines.The first line contains a string S1.The second line contains a string S2.
  1<=T<=10.1<=|S1|,|S2|<=1e6.S1 and S2 only consist of lowercase ,uppercase letter.
 
Output
For each test case,output a single line containing a integer,the answer of test case.
  The answer may be very large, so the answer should mod 1e9+7.
 
Sample Input
2
aaaaa
aa
abababab
aba
 
Sample Output
13
19

Hint

case 2:
Suffix(S2,1) = "aba",
Suffix(S2,2) = "ba",
Suffix(S2,3) = "a".
N1 = 3,
N2 = 3,
N3 = 4.
L1 = 3,
L2 = 2,
L3 = 1.
ans = (3*3+3*2+4*1)%1000000007.

 实际上是枚举第一个字符串 然后看第二个字符串跟他匹配的字符位置最长的地方在哪里
 
 

 伪代码  for(int i=0;i<len;++i)  if(j是跟此时i匹配的最远距离) ans+=f[j]
 
 
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. const int N=1e6+;
  6. const int mod=1e9+;
  7. char chang[N],duan[N];
  8. int nex[N],f[N];
  9. int len1,len2;
  10. void get()
  11. {
  12. int i=,j=-;
  13. nex[]=-;
  14. while(i<len2)
  15. {
  16. if(j==-||duan[i]==duan[j]) nex[++i]=++j;
  17. else j=nex[j];
  18. }
  19. f[]=;
  20. for(int i=; i<=len2; ++i) f[i]=(f[nex[i]]+i)%mod;
  21. }
  22. int KMP()
  23. {
  24. get();
  25. int i=,j=,ans=;
  26. while(i<len1)
  27. {
  28. while(~j&&chang[i]!=duan[j])
  29. {
  30. j=nex[j];
  31. }
  32. ++i;++j;
  33. ans=(ans+f[j])%mod;
  34. if(j==len2) j=nex[j];
  35.  
  36. }
  37. return ans%mod;
  38. }
  39. int main()
  40. {
  41. int T;
  42. for(scanf("%d",&T); T--;)
  43. {
  44. scanf("%s %s",chang,duan);
  45. len1=strlen(chang),len2=strlen(duan);
  46. for(int i=; i<len1/; ++i) swap(chang[i],chang[len1--i]);
  47. for(int i=; i<len2/; ++i) swap(duan[i],duan[len2-i-]);
  48. printf("%d\n",KMP());
  49. }
  50. }

hdu6153KMP的更多相关文章

随机推荐

  1. 【DNS域名解析命令】 nslookup

    1. nslookup作用 nslookup用于查询DNS的记录,查询域名解析是否正常,在网络故障时用来诊断网络问题 nslookup - query Internet name servers in ...

  2. [每日短篇] 1C - Spring Data JPA (0)

    2019独角兽企业重金招聘Python工程师标准>>> 准备把 Spring Data JPA 完整看一遍,顺便把关键要点记录一下.在写这篇文章的今天,再不用 Spring Boot ...

  3. 解决vue渲染时闪烁{{}}的问题

    原文转自: 点我 Vue页面加载时v-show设置的隐藏元素出现导致页面闪烁问题在写APP社区页面的时候在一些地方用了v-show,在刷新页面的时候就发现即便在逻辑判断为false某些元素不该显示时也 ...

  4. 2020年ubuntu1804安装nginx最新稳定版1.16详细教程笔记

    第一次使用nginx是2007年,当时主流还是apache.nginx横空出世,在web2.0的推动下,迅速崛起.眼下已是绝对的主流了. 当时,还有一个轻量级的lighttpd,是德国人写,刚开始还并 ...

  5. python selenium(定位方法)

    一.定位方法 注意:元素属性必须唯一存在 #id定位 find_element_by_id() #name定位 find_element_by_name() #class_name定位 find_el ...

  6. JS中由闭包引发内存泄露的深思

    目录 一个存在内存泄露的闭包实例 什么是内存泄露 JS的垃圾回收机制 什么是闭包 什么原因导致了内存泄露 参考 1.一个存在内存泄露的闭包实例 var theThing = null; var rep ...

  7. eclipse手动添加SVN插件

    最近使用eclipse时,用help下自动下载更新svn总是出错,网上找到手动安装方法,记录下一种可行的 1.手动下载svn插件(百度SVNsite-1.8.18) 2.将下载好的SVNsite-1. ...

  8. 1年之后的拿高工资的资本,Java线程

    只要开启线程,都会开启一块对应的栈内存,然后进行同步执行. -- 谈斌 线程是CPU用来处理程序的资源,线程的执行是抢占式的. 线程开启方式: 创建一个类,继承Thread类.重写 run(), 并在 ...

  9. vue-infinite-scroll------vue的无线滚动插件

    vue-infinite-scroll------vue的无线滚动插件 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 V ...

  10. Flutter 1.17版本重磅发布

    Flutter 1.17 是2020年的第一个稳定版本,此版本包括iOS平台Metal支持(性能更快),新的Material组件,新的Network跟踪工具等等! 对所有人来说,今年是充满挑战的一年. ...