程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 後綴數組之最長公共子串 poj 2774

後綴數組之最長公共子串 poj 2774

編輯:C++入門知識

用後綴數組求兩個串的最長公共子串的長度

詳見羅穗骞的論文


[cpp]
#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <cstdlib>  
#define MAXN 1000005  
using namespace std; 
int r[MAXN]; 
int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXN]; 
int sa[MAXN]; //index range 1~n value range 0~n-1  
int cmp(int *r, int a, int b, int l) 

    return r[a] == r[b] && r[a + l] == r[b + l]; 

void da(int *r, int *sa, int n, int m) 

    int i, j, p, *x = wa, *y = wb, *ws = tmp; 
    for (i = 0; i < m; i++) ws[i] = 0; 
    for (i = 0; i < n; i++) ws[x[i] = r[i]]++; 
    for (i = 1; i < m; i++) ws[i] += ws[i - 1]; 
    for (i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i; 
    for (j = 1, p = 1; p < n; j *= 2, m = p) 
    { 
        for (p = 0, i = n - j; i < n; i++) y[p++] = i; 
        for (i = 0; i < n; i++) 
            if (sa[i] >= j) y[p++] = sa[i] - j; 
        for (i = 0; i < n; i++) wv[i] = x[y[i]]; 
        for (i = 0; i < m; i++) ws[i] = 0; 
        for (i = 0; i < n; i++) ws[wv[i]]++; 
        for (i = 1; i < m; i++) ws[i] += ws[i - 1]; 
        for (i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i]; 
        for (swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++) 
            x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; 
    } 

int rank[MAXN]; //index range 0~n-1 value range 1~n  
int height[MAXN]; //index from 1   (height[1] = 0)  
void calheight(int *r, int *sa, int n) 

    int i, j, k = 0; 
    for (i = 1; i <= n; ++i) rank[sa[i]] = i; 
    for (i = 0; i < n; height[rank[i++]] = k) 
        for (k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; ++k); 
    return; 

char s1[MAXN], s2[MAXN]; 
int main() 

    while(scanf("%s%s", s1, s2) != EOF) 
    { 
        int len = strlen(s1); 
        strcat(s1, "$"); 
        strcat(s1, s2); 
        int n = strlen(s1), m = 0; 
        for(int i = 0; i < n; i++) 
        { 
            m = max(m, (int)s1[i]); 
            r[i] = s1[i]; 
        }  
        r[n] = 0; 
        da(r, sa, n + 1, m + 1); //注意是n + 1 ,原因去看論文  
        calheight(r, sa, n); 
        int res = 0; 
        for(int i = 1; i < n; i++) 
            if((sa[i - 1] < len && sa[i] >= len) || (sa[i - 1] >= len && sa[i] < len)) res = max(res, height[i]); 
        printf("%d\n", res); 
    } 
    return 0; 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#define MAXN 1000005
using namespace std;
int r[MAXN];
int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXN];
int sa[MAXN]; //index range 1~n value range 0~n-1
int cmp(int *r, int a, int b, int l)
{
    return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *r, int *sa, int n, int m)
{
    int i, j, p, *x = wa, *y = wb, *ws = tmp;
    for (i = 0; i < m; i++) ws[i] = 0;
    for (i = 0; i < n; i++) ws[x[i] = r[i]]++;
    for (i = 1; i < m; i++) ws[i] += ws[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i;
    for (j = 1, p = 1; p < n; j *= 2, m = p)
    {
        for (p = 0, i = n - j; i < n; i++) y[p++] = i;
        for (i = 0; i < n; i++)
            if (sa[i] >= j) y[p++] = sa[i] - j;
        for (i = 0; i < n; i++) wv[i] = x[y[i]];
        for (i = 0; i < m; i++) ws[i] = 0;
        for (i = 0; i < n; i++) ws[wv[i]]++;
        for (i = 1; i < m; i++) ws[i] += ws[i - 1];
        for (i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i];
        for (swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
            x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
    }
}
int rank[MAXN]; //index range 0~n-1 value range 1~n
int height[MAXN]; //index from 1   (height[1] = 0)
void calheight(int *r, int *sa, int n)
{
    int i, j, k = 0;
    for (i = 1; i <= n; ++i) rank[sa[i]] = i;
    for (i = 0; i < n; height[rank[i++]] = k)
        for (k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; ++k);
    return;
}
char s1[MAXN], s2[MAXN];
int main()
{
    while(scanf("%s%s", s1, s2) != EOF)
    {
        int len = strlen(s1);
        strcat(s1, "$");
        strcat(s1, s2);
        int n = strlen(s1), m = 0;
        for(int i = 0; i < n; i++)
        {
            m = max(m, (int)s1[i]);
            r[i] = s1[i];
        }
        r[n] = 0;
        da(r, sa, n + 1, m + 1); //注意是n + 1 ,原因去看論文
        calheight(r, sa, n);
        int res = 0;
        for(int i = 1; i < n; i++)
            if((sa[i - 1] < len && sa[i] >= len) || (sa[i - 1] >= len && sa[i] < len)) res = max(res, height[i]);
        printf("%d\n", res);
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved