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 }
  • 相关阅读:
    【字符集及字符编码】UTF-8、UTF-16和UTF-32
    【Android】SQLite基本用法(转)
    【eclipse】导入/导出开发环境(包括编辑器字体颜色大小等)
    一个Android Socket的例子(转)
    Linux中与Windows对应的InterlockedIncrement()函数
    Linux互斥锁pthread_mutex_t
    C++读写文本文件
    C++回调函数调用Java接口抽象函数
    Overlapped I/O模型--事件通知【摘录自《Windows网络编程》】
    Linux C++中需要的头文件
  • 原文地址:https://www.cnblogs.com/hsd-/p/7405365.html
Copyright © 2011-2022 走看看