A Secret

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

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.

 
扩展kmp 模板题
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define mod 1000000007
typedef long long ll;
int T;
char s[],t[];
int extend[],nex[];
void pre_ex_kmp(char x[],int m,int nex[])
{
nex[]=m;
int j=;
while(j+<m&&x[j]==x[j+])
j++;
nex[]=j;
int k=;
for(int i=; i<m; i++)
{
int p=nex[k]+k-;
int L=nex[i-k];
if(i+L<p+)
nex[i]=L;
else
{
j=max(,p-i+);
while(i+j<m&&x[i+j]==x[j])
j++;
nex[i]=j;
k=i;
}
}
}
void ex_kmp(char x[],int m,char y[],int n,int nex[],int extend[])
{
pre_ex_kmp(x,m,nex);
int j=;
while(j<n&&j<m&&x[j]==y[j])
j++;
extend[]=j;
int k=;
for(int i=; i<n; i++)
{
int p=extend[k]+k-;
int L=nex[i-k];
if(i+L<p+)
extend[i]=L;
else
{
j=max(,p-i+);
while(i+j<n&&j<m&&y[i+j]==x[j])
j++;
extend[i]=j;
k=i;
}
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(extend,,sizeof(extend));
memset(nex,,sizeof(nex));
scanf("%s",s);
scanf("%s",t);
int len1=strlen(s);
int len2=strlen(t);
reverse(s,s+len1);
reverse(t,t+len2);
ex_kmp(t,len2,s,len1,nex,extend);
ll ans=;
ll n;
for(int i=; i<len1; i++)
{
if(extend[i])
{
n=extend[i]%mod;
ans=ans+(n*(n+)/)%mod;
ans%=mod; }
}
cout<<ans<<endl;
}
return ;
}

HDU 6153 扩展kmp的更多相关文章

  1. hdu 4333 扩展kmp+kmp重复字串去重

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333 关于kmp next数组求最短重复字串问题请看:http://www.cnblogs.com/z ...

  2. HDU 3336 扩展kmp

    题目大意: 找到字符串中所有和前缀字符串相同的子串的个数 对于这种前缀的问题,通常通过扩展kmp来解决 其实吧这是我第一次做扩展kmp的题目,原来确实看过这个概念,今天突然做到,所以这个扩展kmp的模 ...

  3. hdu 4333(扩展kmp)

    题意:就是给你一个数字,然后把最后一个数字放到最前面去,经过几次变换后又回到原数字,问在这些数字中,比原数字小的,相等的,大的分别有多少个.比如341-->134-->413-->3 ...

  4. HDU 6153 拓展KMP (2017CCPC)

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

  5. HDU 3613 扩展KMP

    暴力枚举大水题,判断回文,扩展KMP #include <cstdio> #include <cstring> #include <algorithm> using ...

  6. HDU 2594 扩展kmp模板题

    题目大意: 给定两个字符串,在第一个字符串中找到一个最大前缀作为第二个字符串的后缀 #include <iostream> #include <cstdio> #include ...

  7. hdu 3613 扩展kmp+回文串

    题目大意:给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串(从左往右或者从右往左读,都一样),那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果 ...

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

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

  9. hdu 6153 思维+扩展kmp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 扩展kmp不理解的看下:http://www.cnblogs.com/z1141000271/p ...

随机推荐

  1. Luogu P3370 【模板】字符串哈希

    方法很多,hash,双hash(个人想到一种三hash),挂链,还有STL: map 乱搞 CODE #include<iostream> #include<map> #inc ...

  2. VMware桥接模式连接局域网

    今天尝试虚拟机直连家里的局域网,用于方便另外一台主机使用家里的虚拟机. 本次连接方式是通过桥接方式,但由于'桥接到'选项默认自动,导致无法连通,最终以下步骤完成配置: 第一步:确认本地网关地址 第二步 ...

  3. Elasticsearch Java Rest Client API 整理总结 (三)——Building Queries

    目录 上篇回顾 Building Queries 匹配所有的查询 全文查询 Full Text Queries 什么是全文查询? Match 全文查询 API 列表 基于词项的查询 Term Term ...

  4. Inno Setup脚本

    某天夜晚一场狂风暴雨,由于办公室座位旁的窗户没关,笔记本电脑泡了一夜水,无法开机,无奈送修,里面的大量资料也不知道会不会丢失. is的脚本只有重新写了,重新研究了一下检测程序是否正在运行的判断方法,另 ...

  5. Selenium--数据驱动(python)

    前言: 什么是数据驱动? 从它的本意来解释,就是数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变.说人话,其实就是参数化. 本次介绍2种文件驱动:ini文件和yaml文件 一.ini文件 1 ...

  6. Java实验报告(实验五)

    课程:Java程序设计                         班级:1351            姓名:王玮怡      学号:20135116 成绩:             指导教师: ...

  7. HashMap相关总结

    1.HashMap:根据键值hashCode值存储数据,大多数情况下可以直接定位到它的值,但是遍历顺序不确定.所有哈希值相同的值存储到同一个链表中                         Ha ...

  8. 剑指offer:二叉树的镜像

    题目描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / ...

  9. wc 统计程序

    WC项目要求 这个项目要求写一个命令行程序,模仿已有的wc.exe的功能,并加以扩充,给出某程序设计源语言文件的字符数.单词数和行数.给实现一个统计程序,它能正确统计程序文件的字符数.单词数.行数,以 ...

  10. image 样式设置

    .image-fluid:响应式大小 .image-thumbnails:照片四周会出现一个1px宽的边框 .figure:用于<figure>标签,用来标记一个图像 .figure-ca ...