【链接】http://acm.hdu.edu.cn/showproblem.php?pid=6153


【题意】


给出两个字符串S1,S2,求S2的所有后缀在S1中出现的次数与其长度的乘积之和。 

【题解】


扩展KMP的模板题.
首先,把S2和S1都倒转一下.
这样就转化成球S2的所有前缀在S1中出现的次数了.
而扩展KMP算法可以求出S1的每个位置i它的后缀i..lens1和S2[0..lens2-1]的最长公共前缀;
因为每个起点都不一样,则不会出现重复计算。
又因为S2的开头字符一定要涉及到;
所以这样是正确的。
根据这个extend[i]可以很容易搞出答案的.

【错的次数】


1

【反思】


(中间乘的时候会爆long long)

【代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define ms(x,y) memset(x,y,sizeof x)
  13. #define ri(x) scanf("%d",&x)
  14. #define rl(x) scanf("%lld",&x)
  15. #define rs(x) scanf("%s",x)
  16. #define oi(x) printf("%d",x)
  17. #define ol(x) printf("%lld",x)
  18. #define oc putchar(' ')
  19. #define os(x) printf(x)
  20. #define all(x) x.begin(),x.end()
  21. #define Open() freopen("F:\\rush.txt","r",stdin)
  22. #define Close() ios::sync_with_stdio(0)
  23.  
  24. typedef pair<int,int> pii;
  25. typedef pair<LL,LL> pll;
  26.  
  27. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  28. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  29. const double pi = acos(-1.0);
  30.  
  31. const int N=1e6;
  32. const LL MOD = 1e9+7;
  33. int Next[N+10],extend[N+10],lent,lens;
  34. char S[N+10],T[N+10];
  35.  
  36. void makenext(int m){
  37. int a = 0;
  38. Next[0] = lens;
  39. while(a < lens - 1 && S[a] == S[a + 1]) a++;
  40. Next[1] = a;
  41. a = 1;
  42.  
  43. for(int k = 2; k < lens; k ++) {
  44. int p = a + Next[a] - 1,L = Next[k - a];
  45. if( (k - 1) + L >= p) {
  46. int j = (p - k + 1) > 0 ? (p - k + 1) : 0;
  47. while(k + j < lens && S[k + j] == S[j]) j++;
  48. Next[k] = j;
  49. a = k;
  50. } else
  51. Next[k] = L;
  52. }
  53. }
  54. void GetNext(const char *T){
  55. int a=0;
  56. int MinLen = lens < lent ? lens : lent;
  57. while(a < MinLen && S[a] == T[a] ) a++;
  58. extend[0]=a;
  59. a=0;
  60. for(int k=1;k < lent;k++){
  61. int p=a+extend[a]-1,L = Next[k-a];
  62. if((k-1)+L>=p){
  63. int j=(p-k+1)>0? (p-k+1):0;
  64. while(k + j < lent && T[k+j] == S[j]) j++;
  65. extend[k]=j;
  66. a=k;
  67. }
  68. else extend[k]=L;
  69. }
  70. }
  71.  
  72. int main(){
  73. //Open();
  74. int TT;
  75. ri(TT);
  76. while (TT--){
  77. rs(T),rs(S);
  78. lent = strlen(T),lens = strlen(S);
  79. reverse(T,T+lent),reverse(S,S+lens);
  80. makenext(lens);
  81. GetNext(T);
  82. LL sum = 0;
  83. rep1(i,0,lent-1){
  84. //extend[i] = min(extend[i],lens);
  85. // 1 + 2 + .. extend[i]
  86. sum = (sum + 1LL*(1 + extend[i])*extend[i]/2)%MOD;
  87. }
  88. ol(sum);puts("");
  89. }
  90. return 0;
  91. }

【2017中国大学生程序设计竞赛 - 网络选拔赛】A Secret的更多相关文章

  1. HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    /* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...

  2. HDU 6150 - Vertex Cover | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    思路来自 ICPCCamp /* HDU 6150 - Vertex Cover [ 构造 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 给了你一个贪心法找最小覆盖的算法,构造一组 ...

  3. HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  4. HDU 6154 CaoHaha's staff(2017中国大学生程序设计竞赛 - 网络选拔赛)

    题目代号:HDU 6154 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 CaoHaha's staff Time Limit: 2000/1 ...

  5. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...

  6. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff(几何找规律)

    Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in ...

  7. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph(暴力搜索)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6152 Problem Description It is well known that small ...

  8. 2017中国大学生程序设计竞赛 - 网络选拔赛 1004 HDU 6153 A Secret (字符串处理 KMP)

    题目链接 Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a presen ...

  9. 2017中国大学生程序设计竞赛 - 网络选拔赛 1005 HDU 6154 CaoHaha's staff (找规律)

    题目链接 Problem Description "You shall not pass!" After shouted out that,the Force Staff appe ...

  10. 2017中国大学生程序设计竞赛 - 网络选拔赛 1003 HDU 6152 Friend-Graph (模拟)

    题目链接 Problem Description It is well known that small groups are not conducive of the development of ...

随机推荐

  1. CentOS下安装C/C++开发工具包的最佳方式

    如果你使用的是 Fedora, Red Hat, CentOS, 或者 Scientific Linux 系统,使用下面的命令安装GNU的C/C++开发包和编译器. # yum groupinstal ...

  2. Vsftp权限控制(持续增加中)

    把用户限制在自己的home目录中,例如限制用户Leon只能访问/home/Leon目录下的文件,不允许访问上级目录. 先打开配置文件 vi /etc/vsftpd/vsftpd.conf 第一种方法: ...

  3. HDU 4358 Boring counting dfs序+莫队算法

    题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...

  4. Sub Thread to update main Thread (UI)

    Sub Thread to update main Thread (UI) main Thread :   A  has Hander.HandleMessage() to process the & ...

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

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

  6. 10G安装DataGuard

    最后更新时间:2013年8月4日,星期日 ★ oracle 10G安装环境 数据库软件安装环境不详细描述,网上到处有这方面资料,下面只简单描述下. 也可参考官方文档: http://docs.orac ...

  7. 【Docker入门】

    目录 Linux容器 Docker的优势 Docker三大概念 安装使用Docker 补充知识 [Docker入门] 发布文章 "qq_41964425" @ *** 所谓Dock ...

  8. Django_高级扩展

  9. Hadoop for .NET Developers

    Hadoop for .NET Developers(一):理解Hadoop 这些年来,大数据已经成为分析业界的兴奋源头.对于这个博客系列的目的,我将松散定义这个术语指的重点是从数据核心业务系统里数据 ...

  10. 【shell学习】经常使用条件推断-字符,数字,文件

    IF 推断 之前也写过简单的shell脚本,也不是转职运维.和系统相关的工作比較少.所以不怎么熟练. 近期因为系统总是出现各种乱七八糟的问题,也没有人来协助.仅仅好自己写shell脚本了,都是些基础的 ...