POJ 3294 life Forms (suffix array to find the oldest string of K-strings)

Source: Internet
Author: User

The main topic: Give n strings, let you find the longest substring, if there are more than the dictionary order output.

Problem-Solving ideas: To connect the n strings, the middle needs to be separated, and then we can enumerate the length of the string, the longest length, if more than one need to follow the dictionary order to save, the final output of the answer. The time complexity is: O (N*log (n)).

Life Forms
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 10275 Accepted: 2822

Description

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 has 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 TheChase. It turns out this 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 is to find the longest substring tha T is shared by more than half of them.

Input

Standard input contains several test cases. Each test case is begins with 1≤ n ≤100 and 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 of the DNA sequence contains at least one and is more than-letters. A line containing 0 follows the last test case.

Output

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

Sample Input

3abcdefgbcdefghcdefghi3xxxyyyzzz0

Sample Output

Bcdefgcdefgh?
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <ctime> #include <map> #include <set> #define EPS 1e-9///#define M 1000100///#define ll __int64#define ll Long long///#define INF 0x7ffffff#define inf 0x3f3f3f3f#define PI 3.1415926535898#define Zero (x) ((FA BS (x) <eps)? 0:x) #define MOD 1000000007#define Read () freopen ("Autocomplete.in", "R", stdin) #define Write () freopen ("    Autocomplete.out "," w ", stdout) #define CIN () Ios::sync_with_stdio (false) using namespace Std;inline int Read () {char ch;    BOOL flag = FALSE;    int a = 0; while (!) ( ((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) | |    (ch = = '-')));        if (ch! = '-') {a *= 10;    A + = ch-' 0 ';    } else {flag = true; } while (((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) {        A *= 10;    A + = ch-' 0 ';    } if (flag) {a = A; } return A;        void write (int a) {if (a < 0) {Putchar ('-');    A =-A;    } if (a >=) {write (A/10); } putchar (a% 10 + ' 0 ');}    const int MAXN = 200010;int WA[MAXN], WB[MAXN], WV[MAXN], ws1[maxn];int sa[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 I, J, p, *x = WA, *y = WB;    for (i = 0; i < m; i++) ws1[i] = 0;    for (i = 0; i < n; i++) ws1[x[i] = r[i]]++;    for (i = 1; i < m; i++) ws1[i] + = ws1[i-1];    for (i = n-1; I >= 0; i--) sa[--ws1[x[i]] = i;        for (j = 1, p = 1; p < n; j <<= 1, 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++) ws1[i] = 0; for (i = 0; i <N        i++) ws1[wv[i]]++;        for (i = 1; i < m; i++) ws1[i] + = ws1[i-1];        for (i = n-1; I >= 0; i--) sa[--ws1[wv[i]] = y[i];    for (Swap (x, y), 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 STR1[MAXN], str2[maxn];int seq[maxn];int hash[maxn];char str[110][1010];struct node{int pos;    int s; int Len;}    F[MAXN], Xf[maxn];int xans;int vis[110];int Find (int x) {int s = hash[x];        for (int i = 0; i < s; i++) {int len = strlen (Str[i]);    X-= Len;    } x-= s; return x;}    BOOL Judge (int mid, int n, int m) {int ans = 0;    int SX;        for (int i = 2; I <= n; i++) {memset (Vis, 0, sizeof (VIS)); SX =1;        VIS[HASH[SA[I-1]] = 1;                while (Height[i] >= mid) {if (!vis[hash[sa[i]]) {vis[hash[sa[i]]] = 1;            ++SX;        } i++;            } if (Sx*2 > m) {xf[ans].len = mid;            Xf[ans].pos = Find (sa[i-1]);        XF[ANS++].S = hash[sa[i-1]];        }} if (ans) {Xans = 0;    for (int i = 0; i < ans; i++) f[xans++] = Xf[i]; } return ans;    void Del (int n, int len, int m) {int L = 1;    int r = Len;    Xans = 0;        while (L <= r) {int mid = (l+r) >>1;        if (Judge (Mid, N, m)) L = mid+1;    else R = mid-1; } if (!xans) {cout<< "?"        <<endl;    Return } for (int i = 0; i < Xans; i++) {for (int j = f[i].pos, k = 0; k < F[i].len; k++, J + +) cout<&        LT;STR[F[I].S][J];    Puts ("");    }}int Main () {int n;    int flag = 0; while (~SCANF ("%d", &n) &Amp;& N) {memset (hash,-1, sizeof (hash));        int ans = 0;        int Min = MAXN;            for (int i = 0; i < n; i++) {scanf ("%s", Str[i]);            int len = strlen (Str[i]);            min = min (min, len);                for (int j = 0; J < Len; J + +) {Seq[ans] = str[i][j];            hash[ans++] = i;        } seq[ans++] = 200+i;        } Seq[ans] = 0;        Da (seq, SA, ans+1, 310);        Calheight (seq, SA, ans);        if (!flag) flag = 1;        Else puts ("");    Del (ans, Min, N); } return 0;}


POJ 3294 life Forms (suffix array to find the oldest string of K-strings)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.