A Secret

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

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中国大学生程序设计竞赛 - 网络选拔赛

题意:

给出两个字符串S1,S2,求S2的所有后缀在S1中匹配到的次数与其后缀长度的乘积之和

思路:

由于乘上了后缀长度,所以对某个后缀的匹配而言,每个字母对答案的贡献是1

由此,我们可以将字符串反转,然后跑一遍kmp,在kmp的过程中统计有多少匹配,并加上他们的贡献即可

代码:

  1 /*
2 * @FileName: D:\代码与算法\2017训练比赛\CCPC网络赛\1004-bt.cpp
3 * @Author: Pic
4 * @Date: 2017-08-19 16:30:09
5 * @Last Modified time: 2017-08-19 20:54:48
6 */
7 #include <bits/stdc++.h>
8 using namespace std;
9 /*
10 * next[]的含义: x[i-next[i]...i-1]=x[0...next[i]-1]
11 * next[i]为满足x[i-z...i-1]=x[0...z-1]的最大z值(就是x的自身匹配)
12 */
13 long long sum=0;
14 const long long mod=1e9+7;
15 const long long MAXN=2e6+30;
16 char x[MAXN],y[MAXN];
17 int nxt[MAXN];
18 void kmp_pre(char x[],int m)
19 {
20 long long i, j;
21 j = nxt[0] = -1;
22 i = 0;
23 while (i < m)
24 {
25 while (-1 != j && x[i] != x[j])j = nxt[j];
26 nxt[++i] = ++j;
27 }
28 }
29 /*
30 * kmpNext[]的意思: next'[i]=next[next[...[next[i]]]] (直到next'[i]<0或者
31 x[next'[i]]!=x[i])
32 * 这样的预处理可以快一些
33 */
34 void KMP_Count( char x[],int m, char y[],int n)
35 { //x是模式串, y是主串
36 long long i, j;
37 kmp_pre(x,m);
38 i = j = 0;
39 while (i < n)
40 {
41 while (-1 != j && y[i] != x[j]) {
42 sum = (sum+((1+j)*j/2)%mod)%mod;
43 j = nxt[j];
44 }
45 i++; j++;
46 //sum=(sum+j)%mod;
47 if (j >= m)
48 {
49 sum = (sum+((1+j)*j/2)%mod)%mod;
50 j = nxt[j];
51 }
52 }
53 while(j>=1){
54 sum = (sum+((1+j)*j/2)%mod)%mod;
55 j=nxt[j];
56 }
57 }
58 int main()
59 {
60 // clock_t startTime,endTime;
61 //startTime = clock();
62 //freopen("data.in","r",stdin);
63 //freopen("out.txt","w",stdout);
64 int t;
65 scanf("%d",&t);
66 while(t--){
67 scanf("%s%s",y,x);
68 int len1=strlen(y);
69 int len2=strlen(x);
70 reverse(x,x+len2);
71 reverse(y,y+len1);
72 sum=0;
73 KMP_Count(x,len2,y,len1);
74 printf("%I64d\n",sum);
75 }
76 //endTime = clock();
77 //cout << "Totle Time : " <<(double)(endTime - startTime)<< "ms" << endl;
78 return 0;
79 }

HDU6513/CCPC2017--A Secret(KMP)的更多相关文章

  1. 【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 ...

  2. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  3. POJ 2406 Power Strings(KMP)

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  4. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  5. codeM编程大赛E题 (暴力+字符串匹配(kmp))

    题目大意:S(n,k)用k(2-16)进制表示1-n的数字所组成的字符串,例如S(16,16)=123456789ABCDEF10: 解题思路: n最大50000,k最大100000,以为暴力会超时. ...

  6. 经典串匹配算法(KMP)解析

    一.问题重述 现有字符串S1,求S1中与字符串S2完全匹配的部分,例如: S1 = "ababaababc" S2 = "ababc" 那么得到匹配的结果是5( ...

  7. URAL 1732 Ministry of Truth(KMP)

    Description In whiteblack on blackwhite is written the utterance that has been censored by the Minis ...

  8. Leetcode28--->字符串的匹配(KMP)

    题目: 题目的本质是给定两个字符串str1,str2,求str1中的str2串开始的地方,即字符串的匹配,KMP算法 思路:时间复杂度为O(m + n),空间复杂度为O(n),原串的长度为m,子串的长 ...

  9. 题解0012:剪花布条(KMP)

    信奥一本通1465 KPM例题 题目链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1465 题目描述:给出花布条和小饰条(字符串),求花布条中能剪 ...

随机推荐

  1. varnish HTTP头

    Cache-Control:指定了缓存如何处理内容.varnish关心max-age参数,并用它来计算对象的TTL.“Cache-Control:no-cache”是被忽略的.Age:varnish添 ...

  2. 数据库设计规范、E-R图、模型图

    (1)数据库设计的优劣: 糟糕的数据库设计: ①数据冗余冗余.存储空间浪费. ②数据更新和插入异常. ③程序性能差. 良好的数据库设计 ①节省数据的存储空间. ②能够保证数据的完整新. ③方便进行数据 ...

  3. 怎样设置cookie生效的域名和路径

    Domain 属性指定浏览器发出HTTP请求时, 哪些域名需要附带这个Cookie. 比如 Domain 设置为 example.com, 那 abc.example.com 也会在发起请求时附带这个 ...

  4. mac 下 vscode配置SFTP连接

    VScode中使用SFTP插件连接远程服务器进行文件修改 下载SFTP插件后,使用Ctrl+Shift+P.输入SFTP,选择第一个将会生成简短的默认配置文件 然后把sftp.json文件内内容换成以 ...

  5. SQL连接(join)

    INNER JOIN:如果表中有至少一个匹配,则返回行 LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行 RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行 FULL JOIN ...

  6. iOS 定义多个参数函数的写法

    多个参数的写法 (方法的数据类型)函数名:(参数1数据类型)参数1的数值的名字 参数2的名字: (参数2数据类型) 参数2值的名字 …. ; 如  :  有三个参数 -(void)getdetailI ...

  7. 如何在Marketing Cloud里创建extension field扩展字段

    首先在Marketing Cloud里找到创建扩展字段的tile入口,搜索关键字extension: 这会进入Fiori应用"Custom fields",能看到系统里所有创建好的 ...

  8. Django_01_创建图书管理项目

    在django中,项目的组织结构为一个项目包含多个应用,一个应用对应一个业务模块 示例:创建项目的名称为test1,完成“图书-英雄”信息的维护,创建应用名称为booktest 创建项目:首先进入到虚 ...

  9. final 在 java 中有什么作用?(未完成)

    final 在 java 中有什么作用?(未完成)

  10. Elasticsearch:运用scroll接口对大量数据实现更好的分页

    在Elasticsearch中,我们可以通过size和from来对我们的结果来进行分页.但是对于数据量很大的索引,这是有效的吗?Scroll API可用于从单个搜索请求中检索大量结果(甚至所有结果), ...