A Secret

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

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
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6160 6159 6158 6157 6156 
 
 
题目大意:
给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少。
题解:
扩展KMP模板题,将s和t串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最长公共前缀存于数组,最后通过将数组的值不为0的进行一个等差数列和的和就可以了。
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+;
const int N = ;
int Next[N];
long long ex[N]; //即extand[]
char p[N],t[N];
int T;
long long ans;
void pre() // next[i]: 以第i位置开始的子串 与 T的公共前缀
{
int lp=strlen(p);
Next[]=lp;
int j=,k=;
while(j+<lp && p[j]==p[j+]) j++;
Next[]=j;
for(int i=; i<lp; i++)
{
int P=Next[k]+k-;
int L=Next[i-k];
if(i+L<P+) Next[i]=L;
else
{
j=max(,P-i+);
while(i+j<lp && p[i+j]==p[j]) j++; // 枚举(p+1,length) 与(p-k+1,length) 区间比较
Next[i]=j;
k=i;
}
}
}
void exkmp()
{
int lp=strlen(p),lt=strlen(t);
pre(); //next数组初始化
int j=,k=;
while(j<lt && j<lp && p[j]==t[j]) j++;
ex[]=j;
for(int i=;i<lt;i++)
{
int P=ex[k]+k-;
int L=Next[i-k];
if(i+L<P+) ex[i]=L;
else
{
j=max(,P-i+);
while(i+j<lt && j<lp && t[i+j]==p[j]) j++;
ex[i]=j;
k=i;
}
}
}
int main()
{ scanf("%d",&T);
while(T--)
{
scanf("%s%s",&t,&p);
int lt=strlen(t);
int lp=strlen(p);
reverse(p,p+lp);
reverse(t,t+lt);
exkmp();
ans=;
for(int i=;i<lt;i++)
ans=(ans+(+ex[i])*ex[i]/)%mod;
printf("%lld\n",ans); }
}

HDU 6153 A Secret(扩展kmp)的更多相关文章

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

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

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

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

  3. HDU 4333 Revolving Digits 扩展KMP

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意:给以数字字符串,移动最后若干位到最前边,统计得到的数字有多少比原来大,有多少和原来同样,有多少 ...

  4. HDU-6153 A Secret 扩展KMP

    题意:求一个字符串的所有后缀在母串中的出现次数*后缀的长度的总和. 题目链接:http://acm.split.hdu.edu.cn/viewcode.php?rid=22147273 思路:先预处理 ...

  5. HDU 3613 Best Reward(扩展KMP求前后缀回文串)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...

  6. HDU 4333 Revolving Digits [扩展KMP]【学习笔记】

    题意:给一个数字,每一次把它的最后一位拿到最前面,一直那样下去,分别求形成的数字小于,等于和大于原来数的个数. SAM乱搞失败 当然要先变SS了 然后考虑每个后缀前长为n个字符,把它跟S比较就行了 如 ...

  7. A - A Secret -扩展KMP

    题目大意: 给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少. 题解: 扩展KMP模板题,将A和B串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最 ...

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

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

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

随机推荐

  1. Mirror--生成用于镜像用户同步的脚本

    USE master GO IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL DROP PROCEDURE sp_hexadecimal GO CREATE PR ...

  2. 【开发者笔记】利用shp2pgsql将shape文件导入到postgresql中

    导入shp文件到postgresql中 1.首先,你需要让shp2pgsql命令可用,百度下载,加入环境变量即可. 下载地址:https://download.osgeo.org/postgis/wi ...

  3. 【转】Deep Learning(深度学习)学习笔记整理系列之(七)

    9.5.Convolutional Neural Networks卷积神经网络 卷积神经网络是人工神经网络的一种,已成为当前语音分析和图像识别领域的研究热点.它的权值共享网络结构使之更类似于生物神经网 ...

  4. 忘记oracle的sys用户密码怎么修改以及Oracle 11g 默认用户名和密码

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  5. poj2262 Goldbach's Conjecture

    poj2262 Goldbach's Conjecture 用欧拉筛把素数筛出来,再枚举一下. #include<iostream> #include<cstdio> #inc ...

  6. office 2010 安装时出错 MSXML版本6.10.1129.0

    微软下载网址:https://www.microsoft.com/zh-cn/download/details.aspx?id=6276&751be11f-ede8-5a0c-058c-2ee ...

  7. Ubuntu屏幕录制工具【转】

    本文转载自:https://blog.csdn.net/Draonly/article/details/74898031 原文参考:https://www.sysgeek.cn/simplescree ...

  8. Nginx反向代理缓冲区优化

    内容目录 proxy_buffering proxy_buffer_size proxy_buffers proxy_busy_buffers_size proxy_max_temp_file_siz ...

  9. Memento(备忘录)

    意图: 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 适用性: 必须保存一个对象在某一个时刻的(部分)状态, 这样以后需要时它 ...

  10. Java Minor GC和FullGC

    Minor GC触发条件:当Eden区满时,触发Minor GC. Full GC触发条件: 调用System.gc时,系统建议执行Full GC,但是不必然执行 老年代空间不足 方法去空间不足 通过 ...