A Secret

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

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.

Source

2017中国大学生程序设计竞赛 - 网络选拔赛

//题意,一个S串,一个T串,问T的每一个后缀串在S串的匹配次数乘后缀串长度之和为多少。

//题解:S,T串逆序后,跑一遍kmp,并且统计匹配度的次数,然后,从lent向前推,因为在匹配时,没有累计T串中T串自己的可匹配情况,

例如逆序后, S: aba T: aba

kmp后,num : 1 1 1 (长为1的匹配数,长为2的匹配数,长为3的匹配数)

而实际,num: 2 1 1 ,因为 aba 匹配成功后,还包含了一次 a 的匹配

所以,最后还要逆序跑一下,利用fail(next)数组来加上,即为答案

  1. # include <cstdio>
  2. # include <cstring>
  3. # include <cstdlib>
  4. # include <iostream>
  5. # include <vector>
  6. # include <queue>
  7. # include <stack>
  8. # include <map>
  9. # include <bitset>
  10. # include <sstream>
  11. # include <set>
  12. # include <cmath>
  13. # include <algorithm>
  14. # pragma comment(linker,"/STACK:102400000,102400000")
  15. using namespace std;
  16. # define LL long long
  17. # define pr pair
  18. # define mkp make_pair
  19. # define lowbit(x) ((x)&(-x))
  20. # define PI acos(-1.0)
  21. # define INF 0x3f3f3f3f3f3f3f3f
  22. # define eps 1e-
  23. # define MOD
  24.  
  25. inline int scan() {
  26. int x=,f=; char ch=getchar();
  27. while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
  28. while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
  29. return x*f;
  30. }
  31. inline void Out(int a) {
  32. if(a<) {putchar('-'); a=-a;}
  33. if(a>=) Out(a/);
  34. putchar(a%+'');
  35. }
  36. #define MX 1000050
  37. /**************************/
  38. int lens,lent;
  39. char S[MX];
  40. char T[MX];
  41. int num[MX];
  42. int fail[MX];
  43.  
  44. void get_next(char * t)
  45. {
  46. int i=,j=-;
  47. fail[]=-;
  48. while(i<lent)
  49. {
  50. if (j==-||T[i]==T[j])
  51. fail[++i]=++j;
  52. else
  53. j=fail[j]; //回溯
  54. }
  55. }
  56.  
  57. void KMP(char *s,char *t)
  58. {
  59. memset(num,,sizeof(num));
  60. get_next(t);
  61. int i=,j=;
  62. while (i<lens)
  63. {
  64. if (j==-||S[i]==T[j])
  65. {
  66. i++,j++;
  67. }
  68. else j = fail[j];
  69.  
  70. if (j!=-) num[j]++;
  71.  
  72. if (j==lent) j = fail[j];
  73. }
  74. }
  75.  
  76. int main()
  77. {
  78. int cas = scan();
  79. while (cas--)
  80. {
  81. scanf("%s",S);
  82. scanf("%s",T);
  83. lens = strlen(S);
  84. lent = strlen(T);
  85. reverse(S,S+lens);
  86. reverse(T,T+lent);
  87. KMP(S,T);
  88. LL ans = ;
  89. for (int i=lent;i>;i--)
  90. {
  91. num[fail[i]]+=num[i];
  92. ans = (ans + ((LL)i*num[i])%MOD)%MOD;
  93. }
  94. printf("%lld\n",ans);
  95. }
  96. return ;
  97. }

A Secret(KMP)的更多相关文章

  1. HDU 6153 A Secret ( KMP&&DP || 拓展KMP )

    题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...

  2. HDU 6153 A Secret (KMP)

    题意:给定两个串,求其中一个串 s 的每个后缀在另一个串 t 中出现的次数. 析:首先先把两个串进行反转,这样后缀就成了前缀.然后求出 s 的失配函数,然后在 t 上跑一遍,如果发现不匹配了或者是已经 ...

  3. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关 ...

  4. 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 ...

  5. HDU 6153 A Secret(扩展kmp)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

  6. HDU 6153 A Secret(扩展KMP模板题)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others) Total ...

  7. 【HDU 6153】A Secret (KMP)

    Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...

  8. HDU6513/CCPC2017--A Secret(KMP)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

  9. A - A Secret -扩展KMP

    题目大意: 给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少. 题解: 扩展KMP模板题,将A和B串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最 ...

随机推荐

  1. Mycat探索之旅(5)----常用的分片规则

    分片枚举 通过在配置文件中配置可能的枚举id,自己配置分片,本规则适用于特定的场景,比如有些业务需要按照省份或区县来做保存, 而全国省份区县固定的,这类业务使用本条规则,配置如下: <table ...

  2. Ubuntu14.04使用samba服务器共享Home目录

    这里记录一下,以Ubuntu 14.04为例.   1.安装samba服务器. sudo apt-get install samba 2.修改配置文件 sudo vim /etc/samba/smb. ...

  3. C# 鼠标全局钩子

    /// <summary> /// 鼠标全局钩子 /// </summary> public class MouseHook { private const int WM_MO ...

  4. 01-Hibernate Tools for Eclipse Plugins安装

    Hibernate Tools for Eclipse Plugins安装 在线安装有两种方法 方法一:"Help > Install New Software Updates&quo ...

  5. python(30)- 常用模块

    模块就是py文件.python中能开辟作用域的只有函数.类和模块. for循环不能开辟作用域,for循环内的变量为全局变量.if...else...同for循环一样. 一 time模块 时间表示形式 ...

  6. ibatis-java.lang.RuntimeException: Error setting property 'setFileSize'

    ibatis查询问题:      ibatis-java.lang.RuntimeException: Error setting property 'setFileSize'

  7. json.Decoder vs json.Unmarshal

    128down voteaccepted It really depends on what your input is. If you look at the implementation of t ...

  8. 2B01-View-Switcher

    Gallery和swithcer联合使用 /* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the ...

  9. layui表格的批量删除功能

    // 批量删除功能 // 1.得到table选中行内容 // 2.得到删除需要的唯一值,一般是id; // 3.将所要删除的项加入到数组中: // 4.判断是否选中: // 5.发送ajax请求,并附 ...

  10. IOS设计模式浅析之桥接模式(Bridge)

    引言 在项目开发中,我们会遇到这样的一种场景:某些类型由于自身的逻辑,往往具有两个或多个维度的变化,比如说大话设计模式书中所说的手机,它有两个变化的维度:一是手机的品牌,可能有三星.苹果等:二是手机上 ...