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位置开始的最 ...
随机推荐
- Django——模板系统过滤器
过滤器,变量的显示形式的改变 一.形式:小写 {{ name | lower }} 二.串联:先转义文本到HTML,再转换每行到 <p> 标签 {{ my_text|escape|line ...
- C++ 字符串转化成浮点型
第一种: char szString[] = "3.1415926535898"; double db1; db1 = atof(szString); printf(" ...
- scp命令使下用
先说下背景把,使用ubuntu,没有windows以下到xshell软件管理一下vps.正瞅着须要下载一个chrome(chrominum真的不好用).此刻大谷歌正墙外呢,无奈挂着ss firefox ...
- WP8数据存储--独立存储设置
<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinition ...
- CentOS 7 中 Docker 的安装和卸载
安装Dokcer Docker 软件包已经包括在默认的 CentOS-Extras 软件源里.因此想要安装 docker,只需要运行下面的 yum 命令: [root@localhost ~]# yu ...
- TCP/IP ---封装与分用
封装 当应用程序用T C P传送数据时,数据被送入协议栈中,然后逐个通过每一层直到被当作一串比特流送入网络.其中每一层对收到的数据都要增加一些首部信息(有时还要增加尾部信息),该过程如图1 - 7所示 ...
- 获取表数据的插入SQL
DECLARE @TABLE_NAME VARCHAR(200) SET @TABLE_NAME = 'myFunction' --表名 DECLARE @TABLE_CONDITION VARCHA ...
- http协议---简述
http(Hypertext transfer protocol)超文本传输协议,通过浏览器和服务器进行数据交互,进行超文本(文本.图片.视频等)传输的规定. 也就是说,http协议规定了超文本传输所 ...
- Redis提供商配置ASP.NET会话状态
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Co ...
- unity3d 动画卡帧 动画合成 动画层次
2013-02-26 16:22 2059人阅读 评论(0) 收藏 举报 unity3d 中动画的添加 http://unity3d.com/support/documentation/Manua ...