UVA 11107

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1000 + 10;

int s[100*maxn];
int sa[100*maxn], n;
int belong[100*maxn];
int vis[105];
//s:(0 ~ m-1)
void build_sa(int m)
{
    int t[100*maxn], t2[100*maxn], c[100*maxn];
    int *x = t, *y = t2;

    for(int i = 0; i < m; i++) c[i] = 0;
    for(int i = 0; i < n; i++) c[x[i] = s[i]]++;
    for(int i = 1; i < m; i++) c[i] += c[i-1];
    for(int i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;

    for(int k = 1; k <= n; k <<= 1){
        int p = 0;

        for(int i = n - k; i < n; i++) y[p++] = i;
        for(int i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;

        for(int i = 0; i < m; i++) c[i] = 0;
        for(int i = 0; i < n; i++) c[x[y[i]]]++;
        for(int i = 0; i < m; i++) c[i] += c[i-1];
        for(int i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];

        swap(x, y);
        p = 1; x[sa[0]] = 0;
        for(int i = 1; i < n; i++){
            x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
        }
        if(p >= n) break;
        m = p;
    }
}

int Rank[maxn*100], height[maxn*100];
void getHeight()
{
    int k = 0;
    for(int i = 0; i < n; i++) Rank[sa[i]] = i;
    for(int i = 0; i < n; i++){
        if(k) k--;
        int j = sa[Rank[i]-1];
        while(s[i+k] == s[j+k]) k++;
        height[Rank[i]] = k;
    }
}

bool isok(int mid, int num, int MAX)
{
    memset(vis, 0, sizeof(vis));
    int cnt = 0;
    for(int i = num+1; i <= n; i++){
        if(mid > height[i]){
            cnt = 0;
            memset(vis, 0, sizeof(vis));
        }
        else{
            if((belong[sa[i-1]] != belong[sa[i]]) && !(vis[belong[sa[i-1]]]&&vis[belong[sa[i]]])){
                cnt++;
                vis[belong[sa[i]]] = vis[belong[sa[i-1]]] = 1;
            }
            if(cnt >= MAX) return true;
        }
    }
    return false;
}

int main()
{
    //freopen("1.in","r",stdin);
    int num;
    int cas = 0;
    while(scanf("%d", &num)!=EOF && num){
        if(cas) printf("\n");
        cas++;
        int ins = 1;
        n = 0;
        int ss = 0;
        for(int i = 0; i < num; i++){
            char temp[maxn];
            scanf("%s", temp);
            if(num == 1) printf("%s\n",temp);
            int len = strlen(temp);
            for(int j = 0; j < len; j++){
                s[n] = temp[j] + 4;
                belong[n] = i+1;
                n++;
            }
            s[n] = ins++;
            belong[n++] = 0;
        }

        if(num == 1) continue;
        s[n] = 0;
        build_sa(130);
        getHeight();

        int l = 0, r = n+1;
        int N = (num)/2;
        while(l < r){
            int mid = (l+r+1)>>1;
            if(isok(mid, num, N)) l = mid;
            else r = mid - 1;
        }

        if(l == 0){
            printf("?\n");
            continue;
        }

        memset(vis, 0, sizeof(vis));
        int cnt = 0;
        int flag = 0;
        for(int i = num+1; i <= n; i++){

            if(l > height[i]){
                cnt = 0;
                flag = 0;
                memset(vis, 0, sizeof(vis));
            }
            else{
                if((belong[sa[i-1]] != belong[sa[i]]) && !(vis[belong[sa[i-1]]]&&vis[belong[sa[i]]])){
                    cnt++;
                    vis[belong[sa[i]]] = vis[belong[sa[i-1]]] = 1;
                }
            }

            if(cnt >= N && !flag){
                for(int j = 0; j < l; j++){
                    char c;
                    c = 'a' + s[sa[i-1]+j] - 101;
                    printf("%c",c);
                }printf("\n");

                flag = 1;
            }
        }
    }
    return 0;
}
posted @ 2017-07-20 20:24  />.<\  阅读(223)  评论(0编辑  收藏  举报