zoukankan      html  css  js  c++  java
  • HDU 6153 扩展kmp

    A Secret

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


    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.
     
    扩展kmp 模板题
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<set>
     7 #include<map>
     8 #include<queue>
     9 #include<stack>
    10 #include<vector>
    11 using namespace std;
    12 #define mod 1000000007
    13 typedef long long ll;
    14 int T;
    15 char s[1000006],t[1000006];
    16 int extend[1000006],nex[1000006];
    17 void pre_ex_kmp(char x[],int m,int nex[])
    18 {
    19     nex[0]=m;
    20     int j=0;
    21     while(j+1<m&&x[j]==x[j+1])
    22         j++;
    23     nex[1]=j;
    24     int k=1;
    25     for(int i=2; i<m; i++)
    26     {
    27         int p=nex[k]+k-1;
    28         int L=nex[i-k];
    29         if(i+L<p+1)
    30             nex[i]=L;
    31         else
    32         {
    33             j=max(0,p-i+1);
    34             while(i+j<m&&x[i+j]==x[j])
    35                 j++;
    36             nex[i]=j;
    37             k=i;
    38         }
    39     }
    40 }
    41 void ex_kmp(char x[],int m,char y[],int n,int nex[],int extend[])
    42 {
    43     pre_ex_kmp(x,m,nex);
    44     int j=0;
    45     while(j<n&&j<m&&x[j]==y[j])
    46         j++;
    47     extend[0]=j;
    48     int k=0;
    49     for(int i=1; i<n; i++)
    50     {
    51         int p=extend[k]+k-1;
    52         int L=nex[i-k];
    53         if(i+L<p+1)
    54             extend[i]=L;
    55         else
    56         {
    57             j=max(0,p-i+1);
    58             while(i+j<n&&j<m&&y[i+j]==x[j])
    59                 j++;
    60             extend[i]=j;
    61             k=i;
    62         }
    63     }
    64 }
    65 int main()
    66 {
    67     scanf("%d",&T);
    68         while(T--)
    69         {
    70             memset(extend,0,sizeof(extend));
    71             memset(nex,0,sizeof(nex));
    72             scanf("%s",s);
    73             scanf("%s",t);
    74             int len1=strlen(s);
    75             int len2=strlen(t);
    76             reverse(s,s+len1);
    77             reverse(t,t+len2);
    78             ex_kmp(t,len2,s,len1,nex,extend);
    79             ll ans=0;
    80             ll n;
    81             for(int i=0; i<len1; i++)
    82             {
    83                 if(extend[i])
    84                 {
    85                     n=extend[i]%mod;
    86                     ans=ans+(n*(n+1)/2)%mod;
    87                     ans%=mod;
    88 
    89                 }
    90             }
    91             cout<<ans<<endl;
    92         }
    93     return 0;
    94 }
  • 相关阅读:
    一个万能的工具包下载网站
    Keras框架简介
    发现了一个非常棒的pyqt5的例子集
    人脸识别常用数据集大全(12/20更新)
    用 opencv和numpy进行图片和字符串互转,并保存至 json
    机器学习中的范数规则化-L0,L1和L2范式(转载)
    利用face_recognition库裁取人脸
    一招解决C盘空间不足,再也不怕硬盘爆满!
    【LeetCode】394.字符串解码(辅助栈、递归、详细图解)
    程序员那些事儿:女婿程序员
  • 原文地址:https://www.cnblogs.com/hsd-/p/7405365.html
Copyright © 2011-2022 走看看