• HDU


    先上题目:

    Longest Common Substring

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


    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
     
      题意:给出两个串,问你这两个串的最长公共子串的长度是多少。
      后缀数组入门题。首先,不得不承认,现在我的水平只可以套一下模板,通过模板我们可以求出sa[],rank[],height[]三个数组。
      对于这里的字符串,我们是从0~n-1。
      sa[i]指的是字典序排第i的后缀的下标是什么,rank[i]指的是原串中第i个后缀(就是从第i个字符开始到末尾的字符串)在后缀数组中排第几。height[i]表示后缀数组中第i个后缀和第i-1一个后缀的最长公共前缀的长度是多少(其中height[0]=0)。
      这里的做法是首先将两个字符串连接起来,在连接处加一个连接符(没在这两个字符串中出现过的字符即可),然后求出height[],再扫描height[],寻找某个同时符合以下要求的值:①比最大值还要大,②suffex(sa[i])和suffex(sa[i-1])分属于两个不同的字符串。这里需要注意每个数组的长度足够。
     
    上代码:
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define MAX 100002
     5 using namespace std;
     6 
     7 char s[(MAX<<1)],b[MAX];
     8 int sa[MAX<<1],rank[MAX<<1],height[MAX<<1],t[MAX<<1],t2[MAX<<1],c[MAX<<1],n,li;
     9 int f[(MAX<<1)];
    10 
    11 void build_sa(int m){
    12     int i,*x=t,*y=t2;
    13     for(i=0;i<m;i++) c[i]=0;
    14     for(i=0;i<n;i++) c[x[i]=s[i]]++;
    15     for(i=0;i<m;i++) c[i]+=c[i-1];
    16     for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
    17     for(int k=1;k<=n;k<<=1){
    18         int p=0;
    19         for(i=n-k;i<n;i++) y[p++]=i;
    20         for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
    21         for(i=0;i<m;i++) c[i]=0;
    22         for(i=0;i<n;i++) c[x[y[i]]]++;
    23         for(i=0;i<m;i++) c[i]+=c[i-1];
    24         for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
    25         swap(x,y);
    26         p=1;    x[sa[0]]=0;
    27         for(i=1;i<n;i++){
    28             x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k] ? p-1 : p++;
    29         }
    30         if(p>=n) break;
    31         m=p;
    32     }
    33     for(i=0;i<n;i++) rank[sa[i]]=i;
    34 }
    35 
    36 void getHeight(){
    37     int i,j,k=0;
    38     for(i=0;i<n;i++){
    39         if(k) k--;
    40         j=sa[rank[i]-1];
    41         while(s[i+k]==s[j+k]) k++;
    42         height[rank[i]]=k;
    43     }
    44 }
    45 
    46 int main()
    47 {
    48     int maxn;
    49     //freopen("data.txt","r",stdin);
    50     while(scanf("%s %s",s,b)!=EOF){
    51         strcat(s,"&");
    52         li=strlen(s);
    53         for(int i=0;i<li-1;i++) f[i]=1;
    54         strcat(s,b);
    55         f[li-1]=0;
    56         n=strlen(s);
    57         for(int i=li;i<n;i++) f[i]=-1;
    58         build_sa(200);
    59         getHeight();
    60         maxn=0;
    61         for(int i=1;i<n;i++){
    62             if(maxn<height[i] && f[sa[i-1]]*f[sa[i]]<0){
    63                 maxn=height[i];
    64             }
    65         }
    66         printf("%d
    ",maxn);
    67     }
    68     return 0;
    69 }
    /*1403*/
  • 相关阅读:
    手机qq2005 没声音
    使用VBS访问外部文本文件一些方法和脚本实例
    sqlserver 备份恢复 学习笔记
    SQL Server中truncate、delete和drop的异同点
    性能诊断
    列整合一例
    XML导入属性数据【经典】
    读取文本行
    利用TcpClient TcpListener 实现发送图片
    德云社的十三香词
  • 原文地址:https://www.cnblogs.com/sineatos/p/3892495.html
Copyright © 2020-2023  润新知