码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 6153 A Secret KMP

时间:2017-08-21 15:52:01      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:eve   sign   print   des   hdu   esc   signed   length   define   

A Secret

 

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.
 

 

题意:

  给定两个串,求其中一个串 s 的每个后缀在另一个串 t 中出现的次数

题解:

  把两个串都 reverse 一下,给 t 做个 KMP 之后让 s 在 KMP 出来的结果上跑一边就好了。

  比赛水过去了

  baabaa
  caabaa

  应该输出23

  重新写过后,KMP还是可以过的

#include<bits/stdc++.h>
using namespace std;
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N=1e6+20,M=1e6+10,inf=2147483647,mod = 1e9+7;

int T,fail[N],sum[N];
char s[N],t[N],a[N],b[N];
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%s%s",a,b);
        for(int i = 0; i < N; ++i) fail[i] = -1,sum[i] = 0;
        int m = strlen(b);
        int n = strlen(a);

        for(int i = 0; i < n; ++i) s[n-i-1] = a[i];

        for(int i = 0; i < m; ++i) t[m-i-1] = b[i];

        int j = -1;
        sum[0] = 1;
        for(int i = 1; i < m; ++i) {
            while(j!=-1&&t[j+1]!=t[i]) j = fail[j];
            if(t[j+1] == t[i]) j++;
            fail[i] = j;
            if(j!=-1)sum[i] = (sum[j] + i+1) % mod;
            else sum[i] = i+1;
        }

        LL ans = 0;
        j = -1;
        for(int i = 0; i < n; ++i) {
            int tmp = j;
            if(j!=-1) ans = (ans + sum[j]) % mod;
            while(j!=-1&&t[j+1]!=s[i]) {
               j = fail[j];
            }
            if(t[j+1] == s[i] && j+1<m) j++;
            if(j == m-1) ans = (ans + j+1) % mod, j = fail[j];
        }
        if(j!=-1) ans = (ans + sum[j]) % mod;
        printf("%lld\n",ans);
    }
    return 0;
}

/*
2
baabaa
caabaa
*/

 

  

HDU 6153 A Secret KMP

标签:eve   sign   print   des   hdu   esc   signed   length   define   

原文地址:http://www.cnblogs.com/zxhl/p/7404490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!