POJ 3294 (UVA 11107) Life Forms 后缀数组

来源:互联网 发布:苹果最新越狱软件 编辑:程序博客网 时间:2024/05/16 09:25

题目大意:

给出n( n <= 100)个字符串, 只包含小写字母, 每个字符串长度不超过1000且不为空

求出最长的子串,满足在n个字符串当中出现在一半以上的字符串上, 如果有多个这样的子串,按字典序输出


大致思路:

简单的后缀数组height数组分组的运用, 首先将所有的串都连接起来, 中间用不同的没有出现在n个字符串中的字符隔开, 然后二分子串长度L判断是否存在满足条件的长度为子串, 对于多个解用vector存储一下其起始位置,最后还原字符串排序后输出即可

一发AC  (~。~)


代码如下:

Result  :  Accepted     Memory  :  3776 KB     Time  :  532 ms

/* * Author: Gatevin * Created Time:  2015/2/11 12:52:18 * File Name: Mononobe_Mitsuki.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define maxn 102333/* * 先将所有串连接起来,中间用不同的没有出现的值隔开,求出后缀数组和height数组后 * 利用height数组进行分组, 二分判断是否存在长度长度为L的串满足条件 * 将满足条件的长度对应的多个不同子串的开始位置保存起来即可, 简单的height数组分组应用 *//* * Doubling Algorithm求后缀数组 */int wa[maxn], wb[maxn], wv[maxn], Ws[maxn];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 *x = wa, *y = wb, *t, i, j, p;    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(t = x, x = y, y = t, 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++;    }    return;}int rank[maxn], height[maxn];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 in[1010];int s[maxn], sa[maxn], belong[maxn];int n;vector <int> pos[1010];//pos[i]保存长度为i的满足条件的串的起始位置,相同的只记录一次bool check(int mid, int N)//查找是否存在满足条件的长度为mid的串{    bool vis[110];    memset(vis, 0, sizeof(vis));    int cnt = 0;    bool ret = false, in = false;    for(int i = 1; i <= N; i++)        if(height[i] >= mid)        {            if(!vis[belong[sa[i]]])                vis[belong[sa[i]]] = 1, cnt++;            if(cnt > (n >> 1))            {                ret = true;                if(!in)                {                    in = true;                    pos[mid].push_back(sa[i]);                }            }        }        else            memset(vis, 0, sizeof(vis)), vis[belong[sa[i]]] = 1, cnt = 1, in = false;    return ret;}int main(){    int N;    bool between = false;    while(scanf("%d", &n), n)    {        N = 0;        if(between) printf("\n");        else between = true;        for(int i = 0; i <= 1000; i++) pos[i].clear();        for(int i = 0; i < n; i++)        {            scanf("%s", in);            int tmp = strlen(in);            for(int j = 0; j < tmp; j++)                belong[N] = i, s[N++] = in[j] - 'a' + 1;            s[N++] = 27 + i;        }        N--;        s[N] = 0;        da(s, sa, N + 1, 130);        calheight(s, sa, N);        int L = 0, R = 1000, ans = 0, mid;        while(L <= R)//二分查找最大的长度mid        {            mid = (L + R) >> 1;            if(check(mid, N))            {                ans = mid;                L = mid + 1;            }            else                R = mid - 1;        }        if(ans == 0)            printf("?\n");        else            //由后缀数组的性质本身已经是字典序升序了            for(unsigned int i = 0; i < pos[ans].size(); i++)            {                for(int j = 0; j < ans; j++)                    printf("%c", s[pos[ans][i] + j] - 1 + 'a');                printf("\n");            }    }    return 0;}







0 0