博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2017中国大学生程序设计竞赛 - 网络选拔赛 1004
阅读量:4595 次
发布时间:2019-06-09

本文共 2550 字,大约阅读时间需要 8 分钟。

A Secret

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)

Total Submission(s): 0    Accepted Submission(s): 0

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.
 
这题需要把字符串反转一下变成前缀,这样就可以用kmp来做了。
在匹配主串时,如果在j的位置,那么副串中就有j个出现,分别是1,2,3,..j所以加上j*(j+1)/2.
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #define ll long long 9 using namespace std;10 const int mod = 1e9+7;11 const int N = 1e6+10;12 char str[N], str1[N];13 int len, len1, nex[N];14 void init() {15 int j = nex[0] = -1;16 int i = 0;17 while(i < len1) {18 if(j == -1 || str1[i] == str1[j]) nex[++i] = ++j;19 else j = nex[j];20 }21 }22 ll kmp() {23 ll ans = 0, i = 0, j = 0;24 while(i < len) {25 if(j == -1 || str[i] == str1[j]) {26 ++i, ++j;27 if(j == len1) {28 ans += j*(j+1)/2;29 ans %= mod;30 j = nex[j];31 }32 } else {33 ans += j*(j+1)/2;34 ans %= mod;35 j = nex[j];36 }37 }38 while(j != -1) {39 ans += j*(j+1)/2;40 ans %= mod;41 j = nex[j];42 }43 return ans;44 }45 int main() {46 int t;47 scanf("%d", &t);48 while(t--) {49 scanf("%s %s",str,str1);50 len = strlen(str), len1 = strlen(str1);51 reverse(str, str+len);52 reverse(str1, str1+len1);53 init();54 printf("%lld\n",kmp());55 }56 return 0;57 }

 

转载于:https://www.cnblogs.com/xingkongyihao/p/7397538.html

你可能感兴趣的文章
checkbox和文字对齐
查看>>
JConsole远程连接配置 服务器监控工具
查看>>
了解HTTP协议栈(实践篇)
查看>>
loj10035. 「一本通 2.1 练习 1」Power Strings
查看>>
%s的用法
查看>>
调用底层不能直接访问的类和方法
查看>>
清理缓存的方法 #DF
查看>>
JAVA array,map 转 json 字符串
查看>>
2017-12-27练习
查看>>
NET设计规范(二) 命名规范
查看>>
VMware 9.0.1安装Mac OS X Mountain Lion 10.8.2
查看>>
SSL延迟
查看>>
android新手关于左右滑动的问题,布局把<android.support.v4.view.ViewPager/><ImageView/> 放在上面就不行了。...
查看>>
深入理解DIP、IoC、DI以及IoC容器
查看>>
赋值文件
查看>>
Vue 数组 字典 template v-for 的使用
查看>>
蓝牙模块选择经验谈
查看>>
java中==和equals
查看>>
CCActionPageTurn3D
查看>>
python random
查看>>