3450--Corporate Identity(后缀数组 多个字符串的最长公共子串)

poj3450-Corporate Identity

题意:给你n个字符串,求n个字符串内的最长公共子串,输出子串。

做法:将n个字符串连接起来,跑一遍sa和height数组,然后二分 公共子串长度,接着判断 height数组是否有连续的大于二分mid值  并且 来自n个不同的字符,维护答案即可。这里把字符转int类型做了,不然会超时

#include
#include
#include
#define rint register int
const int N=1e6+10;
using namespace std;
int s[N];
int y[N],x[N],c[N],sa[N],rk[N],height[N];
int n, num, belong[N], pos, vis[N];
char t[N];
inline void get_SA() {
    for(int i=0;i<=n;++i){
        c[i]=x[i]=y[i]=sa[i]=rk[i]=height[i]=0;
    }
    int m = 5000; //ascll('z')=122
    for(int i=0;i<=m;++i) c[i] = 0;

	for (int i=1; i<=n; ++i) ++c[x[i]=s[i]];//桶
	for (int i=2; i<=m; ++i) c[i]+=c[i-1];//求前缀和
	for (int i=n; i>=1; --i) sa[c[x[i]]--]=i;//逆着标序 得到sa
	for (int k=1; k<=n; k<<=1){ //倍增合并
		int num=0;
		for (int i=n-k+1; i<=n; ++i) y[++num]=i;//没有第二关键字,排名在最前
		for (int i=1; i<=n; ++i) if (sa[i]>k) y[++num]=sa[i]-k;

		for (int i=1; i<=m; ++i) c[i]=0;       //初始化桶
		for (int i=1; i<=n; ++i) ++c[x[i]];    //桶
		for (int i=2; i<=m; ++i) c[i]+=c[i-1]; //求前缀和
		for (int i=n; i>=1; --i) sa[c[x[y[i]]]--]=y[i],y[i]=0;//第二关键字真正的排了序
        for(int i=1;i<=n;++i) y[i] = x[i];
		//swap(x,y);
		x[sa[1]] = num = 1;
		for (int i=2; i<=n; ++i) //对第二关键字进行了简单的桶分类保存至x数组中
			x[sa[i]]=(y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+k]) ? num : ++num;

		if (num==n) break;
		m=num;
	}
	//for (int i=1; i<=n; ++i) printf("%d ", sa[i]);
}
int check(int x,int n)
{
    for(int i=1;i<=n-1;i++)
    {
        if(height[i]=x&&i<=n-1)
        {
            if(!vis[belong[sa[i-1]]])
            {
                vis[belong[sa[i-1]]]=1;
                cnt++;
            }
            i++;
        }
        if(!vis[belong[sa[i-1]]])
        {
            vis[belong[sa[i-1]]]=1;
            cnt++;
        }
        if(cnt>=num)
        {
            pos=sa[i-1];
            return true;
        }
    }
    return false;
}
inline void get_height() {
	int k=0;
	for (int i=1; i<=n; ++i) rk[sa[i]]=i;
	for (int i=1; i<=n; ++i){
		if (rk[i]==1) continue;//第一名height为0
		if (k) --k;   //h[i]>=h[i-1]-1;
		int j=sa[rk[i]-1];
		while (j+k<=n && i+k<=n && s[i+k]==s[j+k]) ++k;
		height[rk[i]]=k;//h[i]=height[rk[i]];
	}
//	puts("");
//	for(int i = 1; i <= n; ++i) printf("%d ",height[i]);
}
inline bool run(int mid)
{

    for(int i = 1; i <= n; ++i){

        if(height[i] < mid) continue;
        for(int j = 0; j <= num; ++j) vis[j] = 0;
        int cnt  = 0;
        while(i <= n && height[i] >= mid){

            if(!vis[belong[sa[i-1]]]){
                vis[belong[sa[i-1]]] = 1;
                cnt++;
            }
            ++i;
        }
        if(!vis[belong[sa[i-1]]]){
            vis[belong[sa[i-1]]] = 1;
            cnt++;
        }
        if(cnt >= num){
            pos = sa[i-1];
            return 1;
        }

    }
    return 0;
}
int main() {
    while(~scanf("%d", &num)){
        if(num == 0) break;
        n = 0;
        int tar = 123;
        for(int i = 1; i <= num; ++i){
            scanf("%s", t + 1);
            int len = strlen(t + 1);
            for(int j = 1; j <= len; ++j){
                s[++n] = t[j] - 'a' + 1;
                belong[n] = i;
            }
            s[++n]=++tar;
            belong[n] = 0;
        }

        s[n+1]=0;
        //cout<> 1;
            if(run(mid)) ans = mid, l = mid + 1;
            else r = mid - 1;
        }
        if(!ans) puts("IDENTITY LOST");
        else{
            for(int i = pos; i < pos + ans; ++i) printf("%c", s[i] - 1 +'a');
            puts("");
        }
    }
}
/*
3
aabbaabb
abbababb
bbbbbabb
*/

 

你可能感兴趣的:(字符串-后缀数组)