博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【HDU 6153】A Secret (KMP)
阅读量:6912 次
发布时间:2019-06-27

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

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

2aaaaaaaabababababa

Sample Output

1319

题解


将两个串反转,再进行普通的kmp匹配。

记录模式串的每个前缀(即反转前的后缀)出现的次数,考虑kmp的next数组,f[i]代表1~i包含1~f[i],那么ans[f[i]]+=ans[i];

参考代码

#include #include 
#include
#include
#include
#include
#include
#include
#include
#define ll long long#define inf 1000000000#define PI acos(-1)#define REP(i,x,n) for(int i=x;i<=n;i++)#define DEP(i,n,x) for(int i=n;i>=x;i--)#define mem(a,x) memset(a,x,sizeof(a))using namespace std;ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}void Out(ll a){ if(a<0) putchar('-'),a=-a; if(a>=10) Out(a/10); putchar(a%10+'0');}const int N=1e6+10;const int MOD=1e9+7;char a[N],b[N];int f[N];ll ans[N];void get_next(){ int m=strlen(b); f[0]=f[1]=0; for(int i=1;i
m) j=0; } }}int main(){ int n=read(); while(n--) { scanf("%s%s",a,b); reverse(b,b+strlen(b)); reverse(a,a+strlen(a)); memset(ans,0,sizeof(ans)); get_next();kmp_count(); int m=strlen(b); for(int i=m;i>=1;i--) ans[f[i]]+=ans[i]; ll sum=0; for(ll i=1;i<=m;i++){ sum=(sum+i*ans[i])%MOD; } printf("%lld\n",sum); } return 0;}

转载于:https://www.cnblogs.com/zsyacm666666/p/7397572.html

你可能感兴趣的文章
C#动态对象(dynamic)示例(实现方法和属性的动态)
查看>>
Objective-C之成魔之路【8-訪问成员变量和属性】
查看>>
支付宝支付-常用支付API详解(查询、退款、提现等)
查看>>
windows下查看特定端口被什么程序占用
查看>>
JSON.parse()与JSON.stringify()的区别
查看>>
1032. Sharing (25)
查看>>
JSP的隐藏对象
查看>>
2014秋C++ 第8周项目 分支程序设计
查看>>
[pig] pig 基础使用
查看>>
java中的线程同步
查看>>
Does the parameter type of the setter match the return type of the getter?
查看>>
MongoDB count distinct group by JavaAPI查询
查看>>
Java多线程系列——原子类的实现(CAS算法)
查看>>
LibEvent代码阅读--多缓冲区和零拷贝技术
查看>>
学生管理系统报错(一)
查看>>
使用 Live555 搭建流媒体服务器
查看>>
第十四周(OOP版电子词典)
查看>>
网络基础知识小小说
查看>>
linux lsof命令详解
查看>>
POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】
查看>>