zoukankan      html  css  js  c++  java
  • hdu6153KMP

    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+50;
    const int mod=1e9+7;
    char chang[N],duan[N];
    int nex[N],f[N];
    int len1,len2;
    void get()
    {
        int i=0,j=-1;
        nex[0]=-1;
        while(i<len2)
        {
            if(j==-1||duan[i]==duan[j]) nex[++i]=++j;
            else j=nex[j];
        }
        f[0]=0;
        for(int i=1; i<=len2; ++i) f[i]=(f[nex[i]]+i)%mod;
    }
    int KMP()
    {
        get();
        int i=0,j=0,ans=0;
        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=0; i<len1/2; ++i) swap(chang[i],chang[len1-1-i]);
            for(int i=0; i<len2/2; ++i) swap(duan[i],duan[len2-i-1]);
            printf("%d
    ",KMP());
        }
    }
  • 相关阅读:
    bzoj3224 普通平衡树
    bzoj 1067 分情况讨论
    bzoj 1269 bzoj 1507 Splay处理文本信息
    bzoj 2733 Splay 启发式合并,名次树
    bzoj1502 simpson求面积
    b_lq_晚会界面单(线段树维护区间最大值表+预留m个位置)
    a_lc_统计子树中城市之间最大距离(枚举子集 + floyd / 2*dfs 求直径)
    b_lq_城市建设 & 公路修建水题 & 新的开始(虚拟结点+MST)
    b_lg_无线通讯网 & 北极通讯网络(问题转化+kruskal)
    b_lg_搭配购买(并查集+01背包)
  • 原文地址:https://www.cnblogs.com/mfys/p/7398193.html
Copyright © 2011-2022 走看看