zoukankan      html  css  js  c++  java
  • HDU 6153 A Secret KMP

    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
    ",ans);
        }
        return 0;
    }
    
    /*
    2
    baabaa
    caabaa
    */

      

  • 相关阅读:
    JAVA-初步认识-第七章-static关键字-数据共享
    JAVA-初步认识-第七章-this关键字应用
    JAVA-初步认识-第七章-this关键字的使用场景二和细节
    JAVA-初步认识-第七章-this关键字的使用场景和原理图解
    未能找到Microsoft.Office.Core.MsoTriState的引用
    Windows Server 2012 FTP配置 后客户机一直登录不上
    ArcEngine 不能再打开其他表了
    CreateFeatureClass 异常,尝试读取或写入受保护的内存 Access
    ArcEngine开发异常:无当前记录
    一个文科妹子的前端悲欢编程之路
  • 原文地址:https://www.cnblogs.com/zxhl/p/7404490.html
Copyright © 2011-2022 走看看