A Secret

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

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

2017中国大学生程序设计竞赛 - 网络选拔赛

//题意,一个S串,一个T串,问T的每一个后缀串在S串的匹配次数乘后缀串长度之和为多少。

//题解:S,T串逆序后,跑一遍kmp,并且统计匹配度的次数,然后,从lent向前推,因为在匹配时,没有累计T串中T串自己的可匹配情况,

例如逆序后, S: aba T: aba

kmp后,num : 1 1 1 (长为1的匹配数,长为2的匹配数,长为3的匹配数)

而实际,num: 2 1 1 ,因为 aba 匹配成功后,还包含了一次 a 的匹配

所以,最后还要逆序跑一下,利用fail(next)数组来加上,即为答案

 # include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
#define MX 1000050
/**************************/
int lens,lent;
char S[MX];
char T[MX];
int num[MX];
int fail[MX]; void get_next(char * t)
{
int i=,j=-;
fail[]=-;
while(i<lent)
{
if (j==-||T[i]==T[j])
fail[++i]=++j;
else
j=fail[j]; //回溯
}
} void KMP(char *s,char *t)
{
memset(num,,sizeof(num));
get_next(t);
int i=,j=;
while (i<lens)
{
if (j==-||S[i]==T[j])
{
i++,j++;
}
else j = fail[j]; if (j!=-) num[j]++; if (j==lent) j = fail[j];
}
} int main()
{
int cas = scan();
while (cas--)
{
scanf("%s",S);
scanf("%s",T);
lens = strlen(S);
lent = strlen(T);
reverse(S,S+lens);
reverse(T,T+lent);
KMP(S,T);
LL ans = ;
for (int i=lent;i>;i--)
{
num[fail[i]]+=num[i];
ans = (ans + ((LL)i*num[i])%MOD)%MOD;
}
printf("%lld\n",ans);
}
return ;
}

A Secret(KMP)的更多相关文章

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

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

  2. HDU 6153 A Secret (KMP)

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

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

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

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

  5. HDU 6153 A Secret(扩展kmp)

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

  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)

    Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...

  8. HDU6513/CCPC2017--A Secret(KMP)

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

  9. A - A Secret -扩展KMP

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

随机推荐

  1. jQuery 文档操作 - insertAfter() ,insertBefore(),after(),before() 方法

    这个方法跟prependTo()和appendTo()不一样的地方在于,一个是仍然插入到元素内部,而insertAfter和insertBefore是插入到元素外部. 这里拿insertBefore来 ...

  2. 一站式学习Wireshark(转载)

    一站式学习Wireshark(一):Wireshark基本用法 2014/06/10 · IT技术 · 4 评论 · WireShark 分享到: 115 与<YII框架>不得不说的故事— ...

  3. Java 连接使用 Redis

    1. 开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java. 首先你需要下载驱动包,下载 jedis.j ...

  4. W25Q128页数和扇区数

    int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size){ *block_size = 4 ...

  5. Jenkins安装和配置系列

    转自:http://www.cnblogs.com/zz0412/tag/jenkins/default.html?page=1 Jenkins进阶系列之——18Jenkins语言本地化    Jen ...

  6. mysql命令:set sql_log_bin=on/off

    对于数据库的操作,经常需要暂时停止对bin-log日志的写入,那就需要这个命令:set sql_log_bin=on/off 参考:dev.mysql.com/doc/refman/5.5/en/se ...

  7. windows system.exe占用文件

    1)问题的原因是出于一个服务Application Experience,如果装好系统后就把此服务设为手动启动了,平时运行也没什么异常.但是win7在运行exe时如果没有这个服务的辅助就会长时间的占用 ...

  8. Keil(MDK-ARM)使用教程(一)_界面+菜单

    Ⅰ.概述 今天总结Keil(MDK-ARM)界面和菜单相关的内容,详情请往下看. 关于Keil的下载.安装和新建工程我已将在前面做了详细的总结,不懂的可以参考我博客里面相关的文章.该文章是在新建好工程 ...

  9. 一种安全云存储方案设计(下)——基于Lucene的云端搜索与密文基础上的模糊查询

    一种安全的云存储方案设计(未完整理中) 一篇老文了,现在看看错漏颇多,提到的一些技术已经跟不上了.仅对部分内容重新做了一些修正,增加了一些机器学习的内容,然并卵. 这几年来,云产品层出不穷,但其安全性 ...

  10. 第一百八十八节,jQuery,选项卡 UI

    jQueryUI,选项卡 UI 学习要点: 1.使用 tabs 2.修改 tabs 样式 3.tabs()方法的属性 4.tabs()方法的事件 5.tabs 中使用 on 选项卡(tab),是一种能 ...