HDU 6153 A Secret
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 817 Accepted Submission(s): 321
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.
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.
The answer may be very large, so the answer should mod 1e9+7.
case 2:
Suffix(S2,1) = "aba",
Suffix(S2,2) = "ba",
Suffix(S2,3) = "a".
N1 = 3,
N2 = 3,
N3 = 4.
L1 = 3,
L2 = 2,
L3 = 1.
ans = (3*3+3*2+4*1)%1000000007.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
const int maxn=1e6+;
ll t,la,lb,i,j,k;
char sa[maxn],sb[maxn];
ll nex[maxn],val[maxn];
void getnext()
{
memset(nex,,sizeof(nex));
memset(val,,sizeof(val));
nex[]=-;
for(i=;i<lb;i++)
{
j=nex[i];
val[i+]=i+;
while(j>-)
{
if(sb[j]==sb[i]) {nex[i+]=j+,val[i+]+=val[j+],val[i+]%=MOD;break;}
j=nex[j];
}
}
/*for(i=1;i<=lb;i++)
{
printf("%lld ",val[i]);
}
printf("\n");*/
}
ll matchfind()
{
ll ans=;
k=;
for(i=;i<la;i++)
{
j=k;
for(k=;j>=;j=nex[j])
{
if(sa[i]==sb[j])
{
k=j+;
break;
}
}
ans=(ans+val[k])%MOD;
}
return ans;
}
int main()
{
scanf("%lld",&t);
while(t--)
{
scanf("%s %s",sa,sb);
la=strlen(sa),lb=strlen(sb);
reverse(sa,sa+la),reverse(sb,sb+lb);
getnext();
printf("%lld\n",matchfind());
}
return ;
}
/*
a b a b b b a b a
next 0 0 1 2 0 0 1 2 3
val 1 2 4 6 5 6 8 10 13
valfj 1 2 3+1 4+2 5 6 7+1 8+2 9+4=9+3+1::next[9]=next[3]=next[1];
*/
HDU 6153 A Secret的更多相关文章
- HDU 6153 A Secret(扩展KMP模板题)
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others) Total ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关 ...
- HDU 6153 A Secret 套路,求解前缀在本串中出现的次数
http://acm.hdu.edu.cn/showproblem.php?pid=6153 首先相当于翻转两个串,然后求s2前缀在s1中出现的次数. 这是一个套路啦 首先把两个串结合起来,中间加一个 ...
- 【kmp或扩展kmp】HDU 6153 A Secret
acm.hdu.edu.cn/showproblem.php?pid=6153 [题意] 给定字符串A和B,求B的所有后缀在A中出现次数与其长度的乘积之和 A和B的长度最大为1e6 方法一:扩展kmp ...
- 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)
题意:给定两个串,求其中一个串 s 的每个后缀在另一个串 t 中出现的次数. 析:首先先把两个串进行反转,这样后缀就成了前缀.然后求出 s 的失配函数,然后在 t 上跑一遍,如果发现不匹配了或者是已经 ...
- HDU 6153 A Secret ( KMP&&DP || 拓展KMP )
题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...
- 【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 ...
随机推荐
- UVa 140 Bandwidth【枚举排列】
题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...
- JavaScript,ES5和ES6的区别
什么是JavaScript JavaScript一种动态类型.弱类型.基于原型的客户端脚本语言,用来给HTML网页增加动态功能.(好吧,概念什么最讨厌了) 动态: 在运行时确定数据类型.变量使用之前不 ...
- MFC的学习路线
首先,MFC是比较难的!比C#和VB要难得多.MFC是基于C++的.首先C++必须熟悉.MFC主要是学习里面的控件的使用. 建议学习路线: 1. 易语言中文编程从入门到精通: https://deta ...
- NOIP 2012 疫情控制(二分+贪心+倍增)
题解 二分时间 然后一个显然的事是一个军队向上爬的越高它控制的点越多 所以首先军队尽量往上爬. 当一个军队可以爬到根节点我们记录下它的剩余时间T和它到达根结点时经过的根节点的子节点son. 当一个军队 ...
- 企业级Nginx+Keepalived集群实战(双主架构)
随着Nginx在国内的发展潮流,越来越多的互联网公司都在使用Nginx,Nginx高性能.稳定性成为IT人士青睐的HTTP和反向代理服务器.Nginx负载均衡一般位于整个网站架构的最前端或者中间层,如 ...
- ECNUOJ 2142 放书
放书 Time Limit:1000MS Memory Limit:65536KBTotal Submit:409 Accepted:173 Description 你要把一叠书放进一些箱子里面,为 ...
- HDU——T 2818 Building Block
http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- berkeley db储存URL队列的简单实现增、删、查
Berkeley DB(BDB)是一个高效的嵌入式数据库编程库,C语言.C++.Java.Perl.Python.Tcl以及其它非常多语言都有其相应的API. Berkeley DB能够保存随意 ...
- HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees plan ...
- IOS 'NSInternalInconsistencyException'
今天想写一个请求的天气.好的.废话不多说.先贴代码: 使用AFNetWorking 发送get请求,可是一直报错 IOS 'NSInternalInconsistencyException', re ...