POJ3294 --- Life Forms (suffix array, binary + suffix group), suffix Array
Description
You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, color, wrinkles, ears, eyebrows and the like. A few bear no human resesponance; 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 incorrect, 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
?
Source
Waterloo Local Contest, 2006.9.30
Concatenate all strings and link them with unused characters in the middle. These characters must be different, and then bind the two answers to the suffix group, check whether the Suffixes in each group appear in more than half of the strings.
/*************************************** * *********************************> File Name: POJ3294.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: ******************************** **************************************** /# include <functional> # include <algorithm> # include <iostream> # include <fstream> # include <cstring> # include <cstdio> # include <cmath> # include <cstd Lib> # include <queue> # include <stack> # include <map> # include <bitset> # include <set> # include <vector> using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; int pos [110100]; char str [1300]; class SuffixArray {public: static const int N = 110100; int init [N]; int X [N]; int Y [N]; int Rank [N]; int sa [N]; int heig Ht [N]; int buc [N]; bool vis [1200]; int size; set <string> st; void clear () {size = 0 ;} void insert (int n) {init [size ++] = n;} bool cmp (int * r, int a, int B, int l) {return (r [a] = r [B] & r [a + l] = r [B + l]);} void getsa (int m = 256) {init [size] = 0; int l, p, * x = X, * y = Y, n = size + 1; for (int I = 0; I <m; ++ I) {buc [I] = 0 ;}for (int I = 0; I <n; ++ I) {++ buc [x [I] = init [I];} For (int I = 1; I <m; ++ I) {buc [I] + = buc [I-1];} for (int I = n-1; i> = 0; -- I) {sa [-- buc [x [I] = I;} for (l = 1, p = 1; l <= n & p <n; m = p, l * = 2) {p = 0; for (int I = n-l; I <n; ++ I) {y [p ++] = I ;}for (int I = 0; I <n; ++ I) {if (sa [I]> = l) {y [p ++] = sa [I]-l ;}} for (int I = 0; I <m; ++ I) {buc [I] = 0 ;}for (int I = 0; I <n; ++ I) {++ buc [x [y [I];} for (int I = 1; I <m; ++ I) {buc [I] + = buc [I-1];} for (int I = n-1; I> = 0; -- I) {sa [-- buc [x [y [I] = y [I];} int I; for (swap (x, y ), x [sa [0] = 0, p = 1, I = 1; I <n; ++ I) {x [sa [I] = cmp (y, sa [I-1], sa [I], l )? P-1: p ++ ;}}void getheight () {int h = 0, n = size; for (int I = 0; I <= n; ++ I) {Rank [sa [I] = I;} height [0] = 0; for (int I = 0; I <n; ++ I) {if (h> 0) {-- h;} int j = sa [Rank [I]-1]; (; I + h <n & j + h <n & init [I + h] = init [j + h]; ++ h ); height [Rank [I]-1] = h ;}} bool check (int k, int n) {int cnt = 1; memset (vis, 0, sizeof (vis); vis [pos [sa [1] = 1; for (int I = 1; I <size; ++ I) {if (height [I]> = k) {if (pos [sa [I + 1]! =-1 &&! Vis [pos [sa [I + 1]) {++ cnt; vis [pos [sa [I + 1] = 1 ;}} else {if (cnt> n/2) {return 1;} memset (vis, 0, sizeof (vis); cnt = 1; if (pos [sa [I + 1]! =-1) {vis [pos [sa [I + 1] = 1 ;}}return 0 ;}void solve (int n) {int l = 1, r = size, mid; int ans = 0; while (l <= r) {mid = (l + r)> 1; if (check (mid, n )) {ans = mid; l = mid + 1;} else {r = mid-1 ;}} if (! Ans) {printf ("? \ N ");} else {st. clear (); int cnt = 1; memset (vis, 0, sizeof (vis); vis [pos [sa [1] = 1; for (int I = 0; I <size; ++ I) {if (height [I]> = ans) {if (! Vis [pos [sa [I + 1]) {++ cnt; vis [pos [sa [I + 1] = 1 ;} for (int j = sa [I + 1]; j <sa [I + 1] + ans; ++ j) {str [j-sa [I + 1] = (char) init [j];} str [ans] = '\ 0'; st. insert (str);} else if (height [I] <ans) {if (cnt> n/2) {set <string >:: iterator it; for (it = st. begin (); it! = St. end (); ++ it) {printf ("% s \ n", it-> c_str () ;}} st. clear (); cnt = 1; memset (vis, 0, sizeof (vis); vis [pos [sa [I + 1] = 1 ;}}}}} SA; int main () {int n; bool flag = 0; while (~ Scanf ("% d", & n), n) {int maxs = 0; SA. clear (); int cnt = 0; for (int I = 1; I <= n; ++ I) {scanf ("% s", str ); int len = strlen (str); for (int j = 0; j <len; ++ j) {SA. insert (int) str [j]); maxs = max (maxs, (int) str [j]); pos [cnt ++] = I;} SA. insert (int) ('Z') + I); pos [cnt ++] =-1;} if (flag) {printf ("\ n ");} else {flag = 1;} SA. getsa (); SA. getheight (); SA. solve (n);} return 0 ;}