A Secret(KMP)
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 830 Accepted Submission(s): 323
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
Sample Output
19
case 2:
Source
//题意,一个S串,一个T串,问T的每一个后缀串在S串的匹配次数乘后缀串长度之和为多少。
//题解:S,T串逆序后,跑一遍kmp,并且统计匹配度的次数,然后,从lent向前推,因为在匹配时,没有累计T串中T串自己的可匹配情况,
例如逆序后, S: aba T: aba
kmp后,num : 1 1 1 (长为1的匹配数,长为2的匹配数,长为3的匹配数)
而实际,num: 2 1 1 ,因为 aba 匹配成功后,还包含了一次 a 的匹配
所以,最后还要逆序跑一下,利用fail(next)数组来加上,即为答案
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
#define MX 1000050
/**************************/
int lens,lent;
char S[MX];
char T[MX];
int num[MX];
int fail[MX]; void get_next(char * t)
{
int i=,j=-;
fail[]=-;
while(i<lent)
{
if (j==-||T[i]==T[j])
fail[++i]=++j;
else
j=fail[j]; //回溯
}
} void KMP(char *s,char *t)
{
memset(num,,sizeof(num));
get_next(t);
int i=,j=;
while (i<lens)
{
if (j==-||S[i]==T[j])
{
i++,j++;
}
else j = fail[j]; if (j!=-) num[j]++; if (j==lent) j = fail[j];
}
} int main()
{
int cas = scan();
while (cas--)
{
scanf("%s",S);
scanf("%s",T);
lens = strlen(S);
lent = strlen(T);
reverse(S,S+lens);
reverse(T,T+lent);
KMP(S,T);
LL ans = ;
for (int i=lent;i>;i--)
{
num[fail[i]]+=num[i];
ans = (ans + ((LL)i*num[i])%MOD)%MOD;
}
printf("%lld\n",ans);
}
return ;
}
A Secret(KMP)的更多相关文章
- HDU 6153 A Secret ( KMP&&DP || 拓展KMP )
题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...
- HDU 6153 A Secret (KMP)
题意:给定两个串,求其中一个串 s 的每个后缀在另一个串 t 中出现的次数. 析:首先先把两个串进行反转,这样后缀就成了前缀.然后求出 s 的失配函数,然后在 t 上跑一遍,如果发现不匹配了或者是已经 ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关 ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 1004 HDU 6153 A Secret (字符串处理 KMP)
题目链接 Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a presen ...
- HDU 6153 A Secret(扩展kmp)
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- HDU 6153 A Secret(扩展KMP模板题)
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others) Total ...
- 【HDU 6153】A Secret (KMP)
Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...
- HDU6513/CCPC2017--A Secret(KMP)
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- A - A Secret -扩展KMP
题目大意: 给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少. 题解: 扩展KMP模板题,将A和B串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最 ...
随机推荐
- C++ x86程序与x64程序中,各种内置类型的大小比较
代码: #include <iostream> #include <cstdio> #include <list> #include <string> ...
- Python——实现代理服务功能
代理服务原理很简单,就拿浏览器与web服务器来说.无非是A浏览器发request给B代理,B代理再把request把送给C web服务,然后C的reponse->B->A.要写web代理服 ...
- ViewPager中Fragment无法显示的问题
问题描述: Actvitiy->Fragment1 ->Fragment2 Fragment1中有1个ViewPager,ViewPager里面有包括了2个Fragment. 当第一次执行 ...
- Python中函数参数传递问题【转】
1. Python passes everything the same way, but calling it "by value" or "by reference& ...
- Intellij IDEA + Tomcat 出现 HTTP status 404错误的解决办法
最近要做POC,接了个老项目改,使用war exploded部署到本机的Tomcat(8.5版) 通过Intellij IDEA启动Tomcat的时候发现系统的登录页面出现HTTP-status-40 ...
- S2S3H4 整合代码示例
主要代码列举: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versi ...
- 【LeetCode】two num 利用comparable接口 对对象进行排序
题目two num 题意:给定一个整数数组和一个目标值.要求在数组中找到两个数.使得它们的和相加等于目标值.而且返回两个数的下标 思路:1.假设使用暴力,时间复杂度为O(n^2) 2.能够先将全部数进 ...
- unity, get Canvas Scaler referenceResolution
需要using UnityEngine.UI; 然后就可以访问到CanvasScaler组件. float width=GetComponent<CanvasScaler> ().refe ...
- Java中几种常见的NPE问题
1.Map下的NPE 直接上代码: Map类集合K/V能不能存储null值的情况,如下表格: 2.foreach遍历集合删除元素 在遍历集合时对元素进行add/remove操作要使用Iterator, ...
- C#动态调用WCF接口(2)
如何使用 1.第一种方式比较简单,而且也是大家喜欢的,因为不需要任何配置文件就可解决,只需知道服务契约接口和服务地址就可以调用. 2.使用Invoke的方式,但是需要在调用客户端配置WCF,配置后在I ...