zoukankan      html  css  js  c++  java
  • 后缀数组:HDU1043 Longest Common Substring

    Longest Common Substring

    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5375    Accepted Submission(s): 1910


    Problem Description
    Given two strings, you have to tell the length of the Longest Common Substring of them.

    For example:
    str1 = banana
    str2 = cianaic

    So the Longest Common Substring is "ana", and the length is 3.
     
    Input
    The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.

    Process to the end of file.
     
    Output
    For each test case, you have to tell the length of the Longest Common Substring of them.
     
    Sample Input
    banana cianaic
     
    Sample Output
    3
     
      这题求最长公共字串,简单的DP并不可以搞定,因为数据范围len<=10^5,所以我用后缀数组做的。
      这里将两个字符串连起来,中间用特殊符号隔开,只要两个在排序上连续的后缀的起点在断点两边,就用其更新答案。
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 
     6 const int maxn=200010;
     7 int Ws[maxn],Wv[maxn],Wa[maxn],Wb[maxn],lcp[maxn],sa[maxn],rank[maxn],r[maxn];
     8 bool cmp(int *p,int i,int j,int l)
     9 {return p[i]==p[j]&&p[i+l]==p[j+l];}
    10 
    11 void DA(int n,int m)
    12 {
    13     int i,j,p,*x=Wa,*y=Wb;
    14     for(i=0;i<m;i++)Ws[i]=0;
    15     for(i=0;i<n;i++)++Ws[x[i]=r[i]];
    16     for(i=1;i<m;i++)Ws[i]+=Ws[i-1];
    17     for(i=n-1;i>=0;i--)
    18         sa[--Ws[x[i]]]=i;
    19     for(j=1,p=1;p<n;j*=2,m=p)
    20     {
    21         for(p=0,i=n-j;i<n;i++)y[p++]=i;
    22         for(i=0;i<n;i++)
    23             if(sa[i]>=j)
    24                 y[p++]=sa[i]-j;
    25         
    26         for(i=0;i<m;i++)Ws[i]=0;
    27         for(i=0;i<n;i++)Wv[i]=x[y[i]];
    28         for(i=0;i<n;i++)++Ws[Wv[i]];
    29         for(i=1;i<m;i++)Ws[i]+=Ws[i-1];
    30         for(i=n-1;i>=0;i--)sa[--Ws[Wv[i]]]=y[i];
    31         for(std::swap(x,y),p=1,i=1,x[sa[0]]=0;i<n;i++)
    32             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;        
    33     }    
    34 }
    35 void LCP(int n)
    36 {
    37     int i,j,k=0;
    38     for(i=1;i<=n;i++)
    39         rank[sa[i]]=i;
    40     
    41     for(i=0;i<n;lcp[rank[i++]]=k)
    42         for(k?--k:k,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);    
    43 }
    44 char str1[maxn>>1],str2[maxn>>1];
    45 int main()
    46 {
    47     int i,len1,len2,ans;
    48     while(~scanf("%s",str1)){
    49         for(len1=0;str1[len1];len1++)
    50             r[len1]=str1[len1];
    51         r[len1]='$';
    52         scanf("%s",str2);
    53         for(len2=0;str2[len2];len2++)
    54             r[len1+len2+1]=str2[len2];
    55         DA(len1+len2+2,128);
    56         LCP(len1+len2+1);
    57         ans=0;
    58         for(i=1;i<len1+len2+1;i++)
    59             if(sa[i]<len1&&sa[i-1]>len1||sa[i-1]<len1&&sa[i]>len1)
    60                 ans=std::max(ans,lcp[i]);
    61         printf("%d
    ",ans);    
    62     }
    63     return 0;
    64 }
     
    尽最大的努力,做最好的自己!
  • 相关阅读:
    【神经网络】LSTM 网络
    【Linux】利用Xvfb关闭chrome的图形化输出
    性能测试面试题:如何找到并发数、平均响应时间、tps的最佳平衡点?
    jmeter引用jar包的3种方式
    flask如何返回真正意义上的json字符串?以及中文如何正常显示?
    记录一次群答问:requests获取cookie
    【笔试题】python文件操作
    JMeter5.1开发http协议接口之form表单脚本
    【笔试题】面向对象小测试(二)
    【笔试题】面向对象小测试(一)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5198379.html
Copyright © 2011-2022 走看看