【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,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
2
aaaaa
aa
abababab
aba
Sample Output
13
19
题解
将两个串反转,再进行普通的kmp匹配。
记录模式串的每个前缀(即反转前的后缀)出现的次数,考虑kmp的next数组,f[i]代表1i包含1f[i],那么ans[f[i]]+=ans[i];
参考代码
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#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;i++){
int j=f[i];
while(j&&b[i]!=b[j]) j=f[j];
f[i+1]=b[i]==b[j]?j+1:0;
}
}
void kmp_count(){
int n=strlen(a),m=strlen(b);
int j=0;
for(int i=0;i<n;i++){
while(j&&b[j]!=a[i]) j=f[j];
if(b[j]==a[i]) j++;
ans[j]++;
if(j==m){
j=f[j];
if(j>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;
}
【HDU 6153】A Secret (KMP)的更多相关文章
- 【hdu 5918】Sequence I(KMP)
给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...
- 【HDU 4763】Theme Section(KMP)
这题数据水的一B.直接暴力都能够过. 比赛的时候暴力过的.回头依照正法做了一发. 匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会非常直接的出,可是KMP的思 ...
- 【HDU 4992】 Primitive Roots (原根)
Primitive Roots Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...
- 【HDU 1533】 Going Home (KM)
Going Home Problem Description On a grid map there are n little men and n houses. In each unit time, ...
- 【HDU - 2102】A计划(bfs)
-->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...
- 【HDU 5839】Special Tetrahedron(计算几何)
空间的200个点,求出至少四边相等,且其余两边必须不相邻的四面体的个数. 用map记录距离点i为d的点有几个,这样来优化暴力的四重循环. 别人的做法是枚举两点的中垂面上的点,再把到中点距离相等的点找出 ...
- 【HDU 4445】Crazy Tank(暴力)
高中物理斜抛运动,简单分析一下角度固定下来则可以计算每个cannonball的降落坐标lnd. 因此暴力计算不同角度下的结果. #include <cstdio> #include &qu ...
- 【HDU 4343】Interval query(倍增)
BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...
- 【HDU 6008】Worried School(模拟)
Problem Description You may already know that how the World Finals slots are distributed in EC sub-r ...
随机推荐
- C# 多线程(转)
C#多线程 一.基本概念 1.进程 首先打开任务管理器,查看当前运行的进程: 从任务管理器里面可以看到当前所有正在运行的进程.那么究竟什么是进程呢? 进程(Process)是Windows系统中的 ...
- yii2 checkbox 的使用实例
使用的是Yii自带的Grid,在使用checkbox希望可以把选中行的id值传到我想要的页面. 首先需要改变key值 $dataProvider->key = 'ID'; button: Htm ...
- Spark MLlib编程API入门系列之特征选择之卡方特征选择(ChiSqSelector)
不多说,直接上干货! 特征选择里,常见的有:VectorSlicer(向量选择) RFormula(R模型公式) ChiSqSelector(卡方特征选择). ChiSqSelector用于使用卡方检 ...
- spring boot使用jpa查询mysql数据库的视图时不报错,但查询结果数据总是重复第一条
问题描述: 在数据库里查询到的结果是正常显示的 在程序中返回的结果: 解决方法: 添加行号作为主键: 解决! 我明明是前端啊前端,为啥在搞后台....,总感觉我要在向全栈进发,希望自己有朝一日真的能成 ...
- Android学习笔记(十七) BroadcastReceiver
1.接收广播 创建一个类,继承BroadcastReceiver,复写其中的onReceive()方法 在AndroidManifest.xml文件中注册该BroadcastReceiver 设置完成 ...
- NSMutableDictionary 排序问题
NSMutableDictionary 默认情况下是按字母的顺序进行排序的 (a-z)的默认排序如何自定义排序呢? 第一种,利用数组的sortedArrayUsingComparator调用 NSCo ...
- 如何开发 Laravel 扩展包并发布到 Composer
如何开发 Laravel 扩展包并发布到 Composer 发布于 2019-01-22 cxp1539 1074 Vie 开发扩展包 我们来做一个根据第一个字符或者汉字生成头像的larave ...
- this.$emit('on-select-change' emit里面不能写大写字母
this.$emit('on-select-change' emit里面不能写大写字母 刚试了下 也能写大写 但是 两边就都写一样就完了,就都写成带-的就完了
- python 人脸识别试水(一)
1.安装python,在这里我的版本是python 3.6 2.安装pycharm,我的版本是pycharm 2017 3.安装pip pip 版本10 4.安装 numpy :pip ins ...
- 把txt格式数据制作成xml数据
txt格式数据: 代码: s1=""" <object> <name>{0}</name> <pose>Unspecifi ...