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位置开始的最 ...
随机推荐
- android EditText inputType 及 android:imeOptions=”actionDone”
一.android 软件盘事件响应 在android中,有时需要对EditText实现软件盘监听的场景.当android按下软键盘的时候,响应完成.发送.搜索或者其他事件. Google 提供了 Ed ...
- AngularJs学习笔记(4)——自定义指令
对指令的第一印象:它是一个自定义标签! 先来看一个简单的指令: <!doctype html> <html ng-app="myApp"> <head ...
- EF中多表公共字段,以及设置EntityBase使所有实体类继承自定义类
使用EF框架访问数据库时,如果某些表具有公共字段,例如在审核流程中,对于各类申请单资料的创建人.创建时间.修改人.修改时间,这些可能多表都需要的字段,如果在每个实体中进行赋值操作显然是类似和重复的,下 ...
- Hive 正则匹配函数
正则匹配字符解释: ^ 表示开头 $ 表示结尾 . 表示任意字符 * 表示任意多个 regexp_extract函数 语法: regexp_extract(string subject, st ...
- Mac终端主题Peppermint
Peppermint主题下载 下载地址:https://noahfrederick.com/log/lion-terminal-theme-peppermint 配置颜色: 1.写配置文件 vim ~ ...
- Atitit.输入法配置说明v1 q229
Atitit.输入法配置说明v1 q229 //------------------------------------------------------ // IME设置 //-------- ...
- UVA 11014 - Make a Crystal(容斥原理)
UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, ...
- Unity3d Serialize问题
备忘: 1. ScriptableOjbect中,由于Serialization的原因,不能使用基类引用来存储子类对象,这样都会导致数据丢失 2. 无法直接对Unity的数据如,vector3, qu ...
- shell脚本中处理 路径中中文和空格方法
OLDIFS=$IFS IFS=$'\n' #存放路径的变量在引用时都使用双引号括起来 "$VAR_PATH" #路径拼接时后续的旧不需要加双引号了"$VAR_PATH& ...
- 锂电池电流单位C5A
C5A是一起的,是一个单位!C5A是描述一个电池本身容量标准中一个放电电流的单位,具体是直观意义是这电池在充满电状态下放电到2.75或者3V,用C5A这么大的电流,能放电5小时,那么C5A就代表的是这 ...