A Secret

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 817    Accepted Submission(s): 321

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

Hint

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.

 
Source
题目容易理解,匹配个数
#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的更多相关文章

  1. HDU 6153 A Secret(扩展KMP模板题)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others) Total ...

  2. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关 ...

  3. HDU 6153 A Secret 套路,求解前缀在本串中出现的次数

    http://acm.hdu.edu.cn/showproblem.php?pid=6153 首先相当于翻转两个串,然后求s2前缀在s1中出现的次数. 这是一个套路啦 首先把两个串结合起来,中间加一个 ...

  4. 【kmp或扩展kmp】HDU 6153 A Secret

    acm.hdu.edu.cn/showproblem.php?pid=6153 [题意] 给定字符串A和B,求B的所有后缀在A中出现次数与其长度的乘积之和 A和B的长度最大为1e6 方法一:扩展kmp ...

  5. 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 ...

  6. HDU 6153 A Secret(扩展kmp)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

  7. HDU 6153 A Secret (KMP)

    题意:给定两个串,求其中一个串 s 的每个后缀在另一个串 t 中出现的次数. 析:首先先把两个串进行反转,这样后缀就成了前缀.然后求出 s 的失配函数,然后在 t 上跑一遍,如果发现不匹配了或者是已经 ...

  8. HDU 6153 A Secret ( KMP&&DP || 拓展KMP )

    题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...

  9. 【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 ...

随机推荐

  1. net实现ping的方法

    class ServicePinger { private static readonly ILog log = LogManager.GetLogger(typeof(ServicePinger)) ...

  2. JACOB调用控件函数

    背景介绍: 使用JAVA程序,实现对系统已安装控件方法的调用. JACOB下载地址:http://danadler.com/jacob/ 使用方法: 1.将jacob.jar添加到项目工程中 2.将j ...

  3. python(1)处理图像

    一提到数字图像处理,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1.不开源,价格贵 2.软件容量大.一般3G以上,高版本甚至达5G以上. 3.只能做研究,不易转化成软件. 因此, ...

  4. PHP 数组转字符串,字符串转数组

    explode将字符串分割为数组: $str = explode( ',',$str); 第一个参数为字符串的分界符,例如1,2,3,4. 第二个是需要分割的数组 分割后就是 array( 1 , 2 ...

  5. [笔记-图论]Floyd

    用于可带负权的多源最短路 时间复杂度O(n^3) 注意一定不要给Floyd一个带负环的图,不然就没有什么意义了(最短路不存在) 模板 // Floyd // to get minumum distan ...

  6. [POI2008]KUP-Plot purchase(单调队列)

    题意 给定k,n,和n*n的矩阵,求一个子矩形满足权值和在[k,2k]之间 , 题解 这里用到了极大化矩阵的思想.推荐论文<浅谈用极大化思想解决最大子矩阵问题>Orz 如果有一个元素在[k ...

  7. qduoj~前端~二次开发

    青岛大学qdu的onlinejudge是js的写的前端,框架是vue.js,在nodejs上部署运行,其实整体运行还是建立在docker的容器虚拟环境里,这里暂时不需要docker.安装环境是Ubun ...

  8. 20180929 北京大学 人工智能实践:Tensorflow笔记08

    https://www.bilibili.com/video/av22530538/?p=28 ———————————————————————————————————————————————————— ...

  9. ArcGIS api for javascript——明确的创建图层列表

    描述 本例展示了如何确切地创建一个地图服务里的图层列表.这个列表由HTML checkboxe组成,可用用于开关图层的可见性. 函数updateLayerVisibility()包含开关图层的逻辑.函 ...

  10. Android中ViewPager的使用

    我们在一个apk中第一次開始的时候,会有一个guide界面.一般使用ViewPager来完毕. 布局文件例如以下: <? xml version="1.0" encoding ...