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的更多相关文章
随机推荐
- 在IBM Cloud中运行Fabric
文章目录 打包智能合约 创建IBM Cloud services 创建fabric网络 创建org和相应的节点 创建order org和相应节点 创建和加入channel 导入智能合约 上篇文章我们讲 ...
- VulnHub靶场学习_HA: Chanakya
HA-Chanakya Vulnhub靶场 下载地址:https://www.vulnhub.com/entry/ha-chanakya,395/ 背景: 摧毁王国的策划者又回来了,这次他创造了一个难 ...
- Java反射机制概念及使用
反射机制 —— 将类中的所有成员反射成对于的类. 以“com.test.Person”类为例 转换对应的类 获取方法 ...
- 【DNS域名解析命令】 nslookup
1. nslookup作用 nslookup用于查询DNS的记录,查询域名解析是否正常,在网络故障时用来诊断网络问题 nslookup - query Internet name servers in ...
- 谈谈你对vuex的理解
vuex创建公有仓库的插件 1.储存公共状态 2.能够根据事件来修改状态 3.多个组件都需要变化,有机制把这个新的状态通知给所有的组件 vuex中的四个类 1.state 定义需要共享的状态 2 ...
- 图论--DFS总结
1.Key word:①双向DFS ②回溯 今天就看到了这么多DFS,其实DFS更倾向于枚举所有情况. 对于双向DFS,我们考虑看看最短路,起点做一下搜索,记录一下到所有点的距离,终点做一下搜索,记 ...
- 小白,你要的Java抽象类,操碎了心!
自从给小白写了两篇科普性质的文章后,我就有点一发不可收拾,觉得很有必要继续写下去.因为有读者留言"鼓励"我说,"二哥,你真的是为小白操碎了心啊!"我容易吗?我. ...
- 在html中使用vue组件
最近在维护公司的项目,当我拿到项目时,发现他用了vue. 但是~~仅仅是引用vue.js文件,整体的架构还是html那种,没有用webpack! 当时觉得~哇~原来还可以这样! 然后了解了业务逻辑和代 ...
- 去 HBase,Kylin on Parquet 性能表现如何?
Kylin on HBase 方案经过长时间的发展已经比较成熟,但也存在着局限性,因此,Kyligence 推出了 Kylin on Parquet 方案(了解详情戳此处).通过标准数据集测试,与仍采 ...
- 多阶段构建Golang程序Docker镜像
Docker简介 Docker是基于Linux容器技术(LXC),使用Go语言实现的开源项目,诞生于2013年,遵循Apache2.0协议.Docker自开源后,受到广泛的关注和讨论. Docker在 ...