POJ2774 Long Long Message

题意

求两个字符串的最长公共子串

题解

两个字符串连在一起,中间加上连接符。
可以证明排序相邻的两个来自不同串的后缀间height最大值即为答案。
思路是假设答案不相邻,来自串1的后缀s1排在来自串2的后缀s2前面,中间可能有若干个来自串2的后缀si,s1与si的LCP一定是不小于s1与s2的(不然s2就排到他们前面了),又因为假设存在一组答案不相邻,所以s1与si的LCP等于s1到s2的LCP。我们取排名最靠前的si,命名为s3,这样s1与s3要么相邻(符合命题和假设),要么中间有很多来自串1的后缀sj,s1与s3的LCP一定不大于sj与s3的LCP,不然s3就排在前面了。由于假设,一定是等于。所以与s3相邻的sj(命名为s4)的LCP长度就是答案了,他们是相邻的。
也就是说,倘若存在不相邻的答案,则一定存在相邻的答案。■

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
void swap(int &a, int &b){int tmp = a;a = b, b = tmp;}
void swap(int* &a, int* &b){int* tmp = a;a = b;b = tmp;}
int max(int a, int b){return a > b ? a : b;}
int min(int a, int b){return a < b ? a : b;}
int lowbit(int x){return x & (-x);}
void read(int &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const int INF = 0x3f3f3f3f;
const int MAXN = 2000000 + 10; 


struct SuffixArray
{
    int s[MAXN], sa[MAXN], rank[MAXN], height[MAXN];
    int t[MAXN], t2[MAXN], c[MAXN];
    int n;
    void clear()
	{
		n = 0;
		memset(sa, 0, sizeof(sa));
	}
    void build_sa(int m)
    {
    	++ n;
        int i,*x = t,*y = t2;
    	for(i = 0;i < m;++ i) c[i] = 0;
    	for(i = 0;i < n;++ i) x[i] = s[i];
    	for(i = 0;i < n;++ i) ++ c[x[i]];
    	for(i = 1;i < m;++ i) c[i] += c[i-1];
    	for(i = n - 1;i >= 0;-- i) sa[-- c[x[i]]] = i;
    	for(int k = 1;k <= n;k <<= 1)
    	{
  			int p = 0;
   	    	for(i = n - k;i < n;++ i) y[p ++] = i;
        	for(i = 0;i < n;++ i) if(sa[i] >= k) y[p ++] = sa[i] - k;
        	for(i = 0;i < m;++ i) c[i] = 0;
        	for(i = 0;i < n;++ i) c[x[i]] ++;
        	for(i = 1;i < m;++ i) c[i] += c[i - 1];
        	for(i = n - 1;i>=0;--i) sa[-- c[x[y[i]]]] = y[i];
        	swap(x,y);
        	p = 1;x[sa[0]] = 0;
        	for(i = 1;i < n;++ i)
        	    x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? p-1 : p++;
        	if(p >= n) break;
        	m = p;
    	}
    	-- n;
    }
    
    void build_height()
    {
        int i, j, k = 0;
   		for(i = 1;i <= n;++ i) rank[sa[i]] = i;
    	for(i = 0;i < n;++ i)
    	{
    	    if(k) k --;
    	    j = sa[rank[i]-1];
    	    while(s[i + k] == s[j + k]) k ++;
    	    height[rank[i]] = k;
    	}
    }
}A;

char s[MAXN];
int ans, n1, n2;

int main()
{
	scanf("%s", s);
	n1 = strlen(s);
	s[n1] = 'z' + 1;
	scanf("%s", s + n1 + 1);
	n2 = strlen(s) - n1 - 1;
	A.n = n1 + n2 + 1;
	for(int i = 0;i < A.n;++ i) A.s[i] = s[i] - 'a' + 1;
	A.build_sa(28);
	A.build_height();
	for(int i = 1;i < A.n;++ i)
		/*if((A.sa[i] < n1 && A.sa[i + 1] > n1)
		|| (A.sa[i + 1] < n1 && A.sa[i] > n1 ))*/
		if((A.sa[i] <= n1) != (A.sa[i + 1] <= n1))
			ans = max(ans, A.height[i + 1]);
	printf("%d", ans);
	return 0;
} 
posted @ 2019-08-03 13:51  嘒彼小星  阅读(273)  评论(0编辑  收藏  举报