A Secret

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

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.

 实际上是枚举第一个字符串 然后看第二个字符串跟他匹配的字符位置最长的地方在哪里
 
 

 伪代码  for(int i=0;i<len;++i)  if(j是跟此时i匹配的最远距离) ans+=f[j]
 
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+;
const int mod=1e9+;
char chang[N],duan[N];
int nex[N],f[N];
int len1,len2;
void get()
{
int i=,j=-;
nex[]=-;
while(i<len2)
{
if(j==-||duan[i]==duan[j]) nex[++i]=++j;
else j=nex[j];
}
f[]=;
for(int i=; i<=len2; ++i) f[i]=(f[nex[i]]+i)%mod;
}
int KMP()
{
get();
int i=,j=,ans=;
while(i<len1)
{
while(~j&&chang[i]!=duan[j])
{
j=nex[j];
}
++i;++j;
ans=(ans+f[j])%mod;
if(j==len2) j=nex[j]; }
return ans%mod;
}
int main()
{
int T;
for(scanf("%d",&T); T--;)
{
scanf("%s %s",chang,duan);
len1=strlen(chang),len2=strlen(duan);
for(int i=; i<len1/; ++i) swap(chang[i],chang[len1--i]);
for(int i=; i<len2/; ++i) swap(duan[i],duan[len2-i-]);
printf("%d\n",KMP());
}
}

hdu6153KMP的更多相关文章

随机推荐

  1. Scala的Higher-Kinded类型

    Scala的Higher-Kinded类型 Higher-Kinded从字面意思上看是更高级的分类,也就是更高一级的抽象.我们先看个例子. 如果我们要在scala中实现一个对Seq[Int]的sum方 ...

  2. HDU 6341 Let Sudoku Rotate

    #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) #defi ...

  3. html+css的用户注册界面

    注册界面样图 代码实现 html部分 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  4. MongoDB学习(四):通过Java使用MongoDB

    环境配置 在Java项目中使用MongoDB,需要在项目中引入mongo.jar这个包.下载地址:下载 请尽量下载较新的版本,本文用的是2.10.1. 连接MongoDB public synchro ...

  5. 数学--数论--POJ281(线性同余方程)

    埃琳娜(Elina)正在阅读刘如家(Rujia Liu)写的书,其中介绍了一种表达非负整数的奇怪方法.方式描述如下: 选择k个不同的正整数a 1,a 2,-,a k.对于一些非负米,把它由每一个我(1 ...

  6. Redis为什么是单线程的

    一.前言   最近在学习Redis,这篇文章就来简单聊聊一道常考的面试题--Redis为什么是单线程的.废话不多说,直接开始吧. 二.正文 2.1 为什么需要多线程   首先,现在的CPU一般都是由多 ...

  7. weak_ptr

    #include <iostream> #include <memory> using namespace std; int main(int argc, char **arg ...

  8. 2020 wannafly camp 补题 day1

    题目可以从牛客上找到. 最简单的一个题应该是B B. 密码学 这个应该就是倒着推,题目给了你加密的顺序,所以我们逆推这个就可以得到每一次加密前的字符串. 1H. 最大公约数 题目大意就是给你一个范围1 ...

  9. CSS的基础使用

    一,css是什么? CSS全称为“层叠样式表” ,与HTML相辅相成,实现网页的排版布局与样式美化 二,CSS使用方式 1.行内样式/内联样式(单一页面中使用) 借助于style标签属性,为当前的标签 ...

  10. js上传文件过大导致上传失败原因以及解决办法

    背景:项目需要用到上传视频功能,由于视频有知识产权,要求必须上传到自己的服务器上不允许用第三方视频网站接口上传,于是一开始开始用的是input type=file去上传,小的视频上传没有问题,上传将近 ...