hdu6153KMP
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 612 Accepted Submission(s): 237
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.
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.
The answer may be very large, so the answer should mod 1e9+7.
aaaaa
aa
abababab
aba
19
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.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+;
const int mod=1e9+;
char chang[N],duan[N];
int nex[N],f[N];
int len1,len2;
void get()
{
int i=,j=-;
nex[]=-;
while(i<len2)
{
if(j==-||duan[i]==duan[j]) nex[++i]=++j;
else j=nex[j];
}
f[]=;
for(int i=; i<=len2; ++i) f[i]=(f[nex[i]]+i)%mod;
}
int KMP()
{
get();
int i=,j=,ans=;
while(i<len1)
{
while(~j&&chang[i]!=duan[j])
{
j=nex[j];
}
++i;++j;
ans=(ans+f[j])%mod;
if(j==len2) j=nex[j]; }
return ans%mod;
}
int main()
{
int T;
for(scanf("%d",&T); T--;)
{
scanf("%s %s",chang,duan);
len1=strlen(chang),len2=strlen(duan);
for(int i=; i<len1/; ++i) swap(chang[i],chang[len1--i]);
for(int i=; i<len2/; ++i) swap(duan[i],duan[len2-i-]);
printf("%d\n",KMP());
}
}
hdu6153KMP的更多相关文章
随机推荐
- vue-cli创建的webpack工程中引用ExtractTextPlugin导致css背景图设置无效的解决方法
当我们用vue-cli创建项目后,如果在我们的template模板文件中的css样式设置中,有设置了background-image的属性,并且url值传入的是相对路径,那么当我们在打包生产代码时,w ...
- 算法竞赛进阶指南--hamilton路径
// hamilton路径 int f[1 << 20][20]; int hamilton(int n, int weight[20][20]) { memset(f, 0x3f, si ...
- 图论--差分约束--POJ 3159 Candies
Language:Default Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 43021 Accep ...
- python基础入门教程(一条龙服务)
一.语言基础 01-1 计算机系统 解释器下载 变量 小整数池 01-2 垃圾回收机制 02 数据类型 运算符(解压赋值等) 03 流程控制 if while for 04 整形 字符串 列表 0 ...
- 如何使用简单 PHP 对象 POPO (Plain old PHP Object) 来优化你的代码
如果您熟悉 Java,可能您已经知道 POJO (普通 Java 类). 因为 POJO 这个词最早是由 Martin Fowler.Rebecca Parsons 和 Josh MacKenzie ...
- 《新版阿里巴巴Java开发手册》提到的三目运算符的空指针问题到底是个怎么回事?
最近,阿里巴巴Java开发手册发布了最新版--泰山版,这个名字起的不错,一览众山小. 新版新增了30+规约,其中有一条规约引起了作者的关注,那就是手册中提到在三目运算符使用过程中,需要注意自动拆箱导致 ...
- Java——Spring依赖配置详解
<properties> <junit.version>4.12</junit.version> <spring.version>4.3.9.RELEA ...
- 系统通配符号、系统正则符号,grep
系统通配符号.系统正则符号,grep 1 系统通配符号 系统通配符号:借助通配符号 匹配文件名称信息 1.1 *: 匹配所有(任意)字符信息 找寻以old开头的文件 find /oldboy -typ ...
- Oracle的pl/sql变量类型
pl/sql定义 sql是结构化查询语言.sql是不是一个编程语言?编程语言一般都能够声明变量,写条件判断,循环.sql不具备这些特征,所有sql不是一门编程语言.我们在实际的开发中,有这种需要,把s ...
- 关于网上quartus ii 生成fft核出现问题解决
------------恢复内容开始------------ 关于网上quartus ii 生成fft核出现问题解决 1:必须把软件破解啦 2:必须把IP核破解啦 破解步骤网上也有可以直接看,一定要全 ...