J - Life Forms POJ - 3294 (后缀数组 + 二分)

You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, ears, eyebrows and the like. A few bear no human resemblance; these typically have geometric or amorphous shapes like cubes, oil slicks or clouds of dust.

The answer is given in the 146th episode of Star Trek - The Next Generation, titled The Chase. It turns out that in the vast majority of the quadrant's life forms ended up with a large fragment of common DNA.

Given the DNA sequences of several life forms represented as strings of letters, you are to find the longest substring that is shared by more than half of them.

Input

Standard input contains several test cases. Each test case begins with 1 ≤ n ≤ 100, the number of life forms. n lines follow; each contains a string of lower case letters representing the DNA sequence of a life form. Each DNA sequence contains at least one and not more than 1000 letters. A line containing 0 follows the last test case.

Output

For each test case, output the longest string or strings shared by more than half of the life forms. If there are many, output all of them in alphabetical order. If there is no solution with at least one letter, output "?". Leave an empty line between test cases.

Sample Input

3
abcdefg
bcdefgh
cdefghi
3
xxx
yyy
zzz
0

Sample Output

bcdefg
cdefgh

?
 

题目大意:给定n个字符串,求出现在不小于k/2个字符串中的最长子串。 

解题思路:按后缀排序的套路把n个字符串拼起来,中间隔一格特殊字符
注意这里每一次隔的特殊字符不能相同

后缀排序求height数组
二分最长公共子串的长度mid在height上判断
即把height按mid值分组
检查一个组内是否有超过n/2隔不同的sa值即可

得出最大长度后再带入height数组找所有答案就好
显然一个组内至多只会有一个可行答案

/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 1e6 + 10 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const int seed = 133 ;

int sa[Maxn] ;
int t1[Maxn], t2[Maxn], c[Maxn] ;
int Rank[Maxn], height[Maxn] ;
int Size, len ;
int re[105], vis[105], ans[Maxn] ;

void getSa(int s[], int n, int m){
    int i, j, p, *x = t1, *y = t2 ;
    for(i = 0; i < m; i++) c[i] = 0 ;
    for(i = 0; i < n; i++) c[x[i] = s[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(j = 1; j <= n; j <<= 1){
        p = 0 ;
        for(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 < m; i++) c[i] = 0 ;
        for(i = 0; i < n ; i++) c[x[y[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 - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p-1 : p++ ;
        if(p >= n) break ;
        m = p ;
    }
}

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

char str[Maxn] ;
int s[Maxn] ;

int check (int mid, int len, int k){
//    cout << mid << " " << len << " " << k << endl ;
    int index = 0, cnt = 0 ;
    memset(vis, 0, sizeof (vis)) ;
    for (int i = 1; i <= len; i++){
        if (height[i] >= mid){
            for (int j = 1; j <= k; j++){
                if (sa[i] > re[j - 1] && sa[i] < re[j]) {
                    cnt += (vis[j] ? 0 : 1) ;
                    vis[j] = 1 ;
                }
                if (sa[i - 1] > re[j - 1] && sa[i - 1] < re[j]) {
                    cnt += (vis[j] ? 0 : 1) ;
                    vis[j] = 1 ;
                }
            }
        }
        else {
            if (cnt > k / 2) ans[++index] = sa[i - 1] ;
            cnt = 0 ;
            memset(vis, 0, sizeof (vis)) ;
        }
    }
    if (cnt > k / 2) ans[++index] = sa[len] ;
    if (index){
        ans[0] = index ;
        return 1 ;
    }
    return 0 ;
}

int main(){
    int k, flag = 0 ;
    while (~scanf("%d", &k) && k){
        len = 0 ;
        Size = 0 ;
        for(int i = 1; i <= k; i++){
            scanf("%s",str + len);
            for(; str[len] != '\0'; len++)  s[len] = str[len] ;
            s[len] = '#' + i ;
            re[++Size] = len ;
            len++ ;
        }
        s[len - 1] = 0 ;
        getSa(s, len, 255) ;
        getHeight(s, len - 1) ;
//        for (int i = 1; i <= len; i++){
//            cout << sa[i] << " " ;
//        }
//        cout << endl ;
//        for (int i = 1; i <= len; i++){
//            cout << Rank[i] << " " ;
//        }
//        cout << endl ;
        int l = 1, r = len ;
//        int ansMid = 0 ;
        while (l <= r){
            int mid = (l + r) >> 1 ;
            if (check(mid, len, k)) {
                l = mid + 1 ;
//                ansMid = mid ;
            }
            else r = mid - 1 ;
        }
        if (flag) puts("") ;
        flag = 1 ;
        if (l == 1) puts("?") ;
        else {
            for (int i = 1; i <= ans[0]; i++){
                for (int j = ans[i]; j < ans[i] + l - 1; j++){
                    printf("%c", s[j]) ;
                }
                puts("") ;
            }
        }
    }
    return 0;
}

 

上一篇:英语情话古风版,没想到这么好听,最爱那句"行也思君,坐也思君"


下一篇:A Bug's Life POJ 2492