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. 分布式锁(redis/mysql)

    单台机器所能承载的量是有限的,用户的量级上万,基本上服务都会做分布式集群部署.很多时候,会遇到对同一资源的方法.这时候就需要锁,如果是单机版的,可以利用java等语言自带的并发同步处理.如果是多台机器 ...

  2. CUDA编程学习相关

    1. CUDA编程之快速入门:https://www.cnblogs.com/skyfsm/p/9673960.html 2. CUDA编程入门极简教程:https://blog.csdn.net/x ...

  3. MySQL命令1

    开始学习MySQL. // 创建数据库 CREATE DATABASE db_name; // 删除数据库 DROP DATABASE db_name; // 显示数据库 SHOW DATABASES ...

  4. Shell简单实现多线程

        一.目的 解决Shell脚本单线程下效率低下的问题 二.适用场景 需要在Linux系统执行同一项命令,但是针对不同的对象,例如PING检测主机,当然可以延展,只要是命令之间不会产生冲突就可以了 ...

  5. P2765 魔术球问题 网络流二十四题重温

    P2765 魔术球问题 知识点::最小点覆盖 这个题目要拆点,这个不是因为每一个球只能用一次,而是因为我们要求最小点覆盖,所以要拆点来写. 思路: 首先拆点,然后就是开始建边,因为建边的条件是要求他们 ...

  6. 将csv文件导入sql数据库

    有一个csv文件需要导入到Sql数据库中,其格式为 “adb”,"dds","sdf" “adb”,"dds","sdf" ...

  7. Day_08【面向对象】扩展案例3_使用多态的形式创建缉毒狗对象,调用缉毒方法和吼叫方法

    分析以下需求,并用代码实现: 1.定义动物类: 行为: 吼叫:没有具体的吼叫行为 吃饭:没有具体的吃饭行为 2.定义缉毒接口 行为: 缉毒 3.定义缉毒狗:犬的一种 行为: 吼叫:汪汪叫 吃饭:狗啃骨 ...

  8. FOC:在MCU上检验Clark和Park坐标变换是否正确

    文章目录 前言 程序 头文件 clark 变换 C实现 park c 变换实现 仿真 前言 仿真简单,可以参考仿真的结果,但是实际中将代码移植到MCU,会出现一些新的问题,所以需要对坐标变换部分算法进 ...

  9. flush方法和close方法的区别

    package com.yhqtv.demo05.Writer; import java.io.FileWriter; /* * @author XMKJ yhqtv.com Email:yhqtv@ ...

  10. [hdu5521 Meeting]最短路

    题意:有N个点,给定M个集合,集合Si里面的点两两之间的距离都为Ti,集合里面的所有点数之和<=1e6.有两个人分别在1和N处,求1个点使得两个人到这一点距离的最大值最小 思路:这题是裸的最短路 ...