博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 6153 拓展KMP (2017CCPC)
阅读量:5830 次
发布时间:2019-06-18

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

 

A Secret

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

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.

 

 

题意

给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少。

思路

扩展KMP模板题,将s和t串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最长公共前缀存于数组,最后通过将数组的值不为0的进行一个等差数列和的和就可以了。

 

代码:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 const int maxn = 1e6 + 10; 8 const int mod = 1e9 + 7; 9 typedef long long ll;10 int cnt[maxn];11 char A[maxn],B[maxn];12 int Next[maxn],ex[maxn];13 ll add(ll n)14 {15 ll m=((n%mod)*((n+1)%mod)/2)%mod;16 return m;17 }18 void kmp(char P[])19 {20 int m=strlen(P);21 Next[0]=m;22 int j=0,k=1;23 while(j+1

 

转载于:https://www.cnblogs.com/mj-liylho/p/7400064.html

你可能感兴趣的文章
WindowManager.LayoutParams 详解
查看>>
find的命令的使用和文件名的后缀
查看>>
Android的Aidl安装方法
查看>>
Linux中rc的含义
查看>>
曾鸣:区块链的春天还没有到来| 阿里内部干货
查看>>
如何通过Dataworks禁止MaxCompute 子账号跨Project访问
查看>>
js之无缝滚动
查看>>
Django 多表联合查询
查看>>
logging模块学习:basicConfig配置文件
查看>>
Golang 使用 Beego 与 Mgo 开发的示例程序
查看>>
+++++++子域授权与编译安装(一)
查看>>
asp.net怎样在URL中使用中文、空格、特殊字符
查看>>
路由器发布服务器
查看>>
实现跨交换机VLAN间的通信
查看>>
python例子
查看>>
环境变量(总结)
查看>>
ios之UILabel
查看>>
Java基础之String,StringBuilder,StringBuffer
查看>>
1月9日学习内容整理:爬虫基本原理
查看>>
安卓中数据库的搭建与使用
查看>>