POJ3294---Life Forms (suffix array, binary + suffix group)

Source: Internet
Author: User

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 The Chase. 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

3
Abcdefg
Bcdefgh
Cdefghi
3
Xxx
yyy
zzz
0

Sample Output

Bcdefg
Cdefgh

?

Source
Waterloo Local Contest, 2006.9.30

Connect the strings together, in the middle with no occurrences of the word Fulianqi, these characters to be different, and then the two-point answer, to the suffix group, see whether the suffix in each group appears in more than half of the string

/************************************************************************* > File Name:POJ3294.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 April 03 Friday 21:07 09 Seconds ******************************** ****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;intpos[110100];Charstr[1300];classsuffixarray{ Public:Static Const intN =110100;intInit[n];intX[n];intY[n];intRank[n];intSa[n];intHeight[n];intBuc[n];BOOLvis[ -];intSize Set <string>QtyvoidClear () {size =0; }voidInsertintN) {init[size++] = n; }BOOLcmpint*r,intAintBintL) {return(R[a] = = R[b] && r[a + l] = = R[b + L]); }voidGetsa (intm = the) {Init[size] =0;intL, p, *x = x, *y = y, n = size +1; for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n;            ++i) {++buc[x[i] = init[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = 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(inti = n-l; I < n;                ++i) {y[p++] = i; } for(inti =0; I < n; ++i) {if(Sa[i] >= L)                    {y[p++] = sa[i]-l; }                } for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n;                ++i) {++buc[x[y[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0;                -i) {sa[--buc[x[y[i]]] = y[i]; }intI 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++; }            }        }voidGetHeight () {inth =0, n = size; for(inti =0; I <= N;            ++i) {Rank[sa[i]] = i; } height[0] =0; for(inti =0; I < n; ++i) {if(H >0) {--h; }intj = Sa[rank[i]-1]; for(; i + H < n && j + H < n && init[i + h] = = Init[j + h]; ++h); Height[rank[i]-1] = h; }        }BOOLCheckintKintN) {intCNT =1;memset(Vis,0,sizeof(VIS)); vis[pos[sa[1]]] =1; for(inti =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; }voidSolveintN) {intL =1, r = size, mid;intAns =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 ();intCNT =1;memset(Vis,0,sizeof(VIS)); vis[pos[sa[1]]] =1; for(inti =0; i < size; ++i) {if(Height[i] >= ans) {if(!vis[pos[sa[i +1]]) {++cnt; Vis[pos[sa[i +1]]] =1; } for(intj = sa[i +1]; J < Sa[i +1] + ans; ++J) {str[j-sa[i +1]] = (Char) Init[j]; } Str[ans] =' + ';                    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;intMain () {intNBOOLFlag =0; while(~scanf("%d", &n), N) {intMaxs =0; Sa.clear ();intCNT =0; for(inti =1; I <= N; ++i) {scanf('%s ', str);intLen =strlen(str); for(intj =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;}

POJ3294---Life Forms (suffix array, binary + suffix group)

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.