UVA11107 Life Forms SA模板

Life Forms
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 16827   Accepted: 4943

Description

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

?

Source

 

【题解】

UVA炸了,在POJ上交的

把所有的串连起来,串尾加分隔符,一来识别串尾,二来防止跟后面的串拼起来一块与其他串组成LCP,分组时会出现错误

二分答案分组,看每个组里是否有大于n/2个串即可

细节很多,关键是要输出方案。。。还要做很鬼畜的标记,各种判断标识符。。

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <vector>
  8 #include <cmath> 
  9 #define min(a, b) ((a) < (b) ? (a) : (b))
 10 #define max(a, b) ((a) > (b) ? (a) : (b))
 11 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
 12 template <class T>
 13 inline void swap(T& a, T& b)
 14 {
 15     T tmp = a;a = b;b = tmp;
 16 }
 17 inline void read(int &x)
 18 {
 19     x = 0;char ch = getchar(), c = ch;
 20     while(ch < '0' || ch > '9') c = ch, ch = getchar();
 21     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
 22     if(c == '-') x = -x;
 23 }
 24 const int INF = 0x3f3f3f3f;
 25 const int MAXN = 1000000 + 10;
 26 struct SuffixArray
 27 {
 28     char s[MAXN];int sa[MAXN], t1[MAXN], t2[MAXN], rank[MAXN], height[MAXN], c[MAXN], n;
 29     void clear(){n = 0;memset(sa, 0, sizeof(sa));}
 30     void build_sa(int m)
 31     {
 32         int i, *x = t1, *y = t2;
 33         for(i = 0;i <= m;++ i) c[i] = 0;
 34         for(i = 1;i <= n;++ i) ++ c[x[i] = s[i]];
 35         for(i = 1;i <= m;++ i) c[i] += c[i - 1];
 36         for(i = n;i >= 1;-- i) sa[c[x[i]] --] = i;
 37         for(int k = 1;k <= n;k <<= 1)
 38         {
 39             int p = 0;
 40             for(i = n - k + 1;i <= n;++ i) y[++ p] = i;
 41             for(i = 1;i <= n;++ i) if(sa[i] > k) y[++ p] = sa[i] - k;
 42             for(i = 0;i <= m;++ i) c[i] = 0;
 43             for(i = 1;i <= n;++ i) ++ c[x[y[i]]];
 44             for(i = 1;i <= m;++ i) c[i] += c[i - 1];
 45             for(i = n;i >= 1;-- i) sa[c[x[y[i]]] --] = y[i];
 46             swap(x, y);p = 0,x[sa[1]] = ++ p;
 47             for(i = 2;i <= n;++ i) x[sa[i]] = sa[i] + k <= n && sa[i - 1] + k <= n && y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k] ? p : ++ p;
 48             if(p >= n) break;m = p;
 49         }
 50     }
 51     void build_height()
 52     {
 53         int i,j,k = 0;
 54         for(i = 1;i <= n;++ i) rank[sa[i]] = i;
 55         for(i = 1;i <= n;++ i)
 56         {
 57             if(k) -- k; if(rank[i] == 1) continue;
 58             j = sa[rank[i] - 1];
 59             while(i + k <= n && j + k <= n && s[i + k] == s[j + k]) ++ k;
 60             height[rank[i]] = k;
 61         }
 62     }
 63 }A;
 64 int cnt, n, tmp, ans, num[MAXN], vis[MAXN], tt = 1;
 65 std::vector<int> node;
 66 int check(int x)
 67 {
 68     int num = 0, flag = 0, t = 1, f = 1;
 69     for(int i = 1;i <= n;++ i)
 70     {
 71         if(A.s[A.sa[i]] == 'z' + 1 || A.s[A.sa[i - 1]] == 'z' + 1 || ::num[A.sa[i] + x] != ::num[A.sa[i]] || ::num[A.sa[i - 1] + x] != ::num[A.sa[i - 1]]) 
 72         {
 73             num = 0, ++ tt, t = 1, f = 1;
 74             continue; 
 75         }
 76         if(A.height[i] >= x) 
 77         {
 78             if(t)
 79             {
 80                 t = 0;
 81                 vis[::num[A.sa[i - 1]]] = tt;
 82                 ++ num;
 83             }
 84             if(vis[::num[A.sa[i]]] == tt) continue;
 85             ++ num, vis[::num[A.sa[i]]] = tt;
 86         }
 87         else num = 0, ++ tt, t = 1, f = 1;
 88         if(num >= cnt/2 + 1 && f) 
 89         {
 90             if(!flag) node.clear(); 
 91             node.push_back(i);
 92             f = 0;
 93             flag =1;
 94         }
 95     }    
 96     return flag;
 97 }
 98 void put()
 99 {
100     for(int i = 0;i < node.size();++ i)
101     {
102         for(int j = A.sa[node[i]], k = 1;k <= ans;++ k, ++ j)
103             printf("%c", A.s[j]);
104         putchar('\n');
105     }
106 }
107 int main()
108 {
109     while(scanf("%d", &cnt) != EOF && cnt)
110     {
111         if(cnt == 1)
112         {
113             scanf("%s", A.s + 1);
114             printf("%s\n\n", A.s + 1);
115             continue;
116         }
117         A.clear();n = 0;tmp = 0;
118         for(int i = 1;i <= cnt;++ i)
119         {
120             scanf("%s", A.s + n + 1);
121             int t = n + 1;
122             tmp = max(tmp, strlen(A.s + n + 1));
123             n += strlen(A.s + n + 1);
124             A.s[++ n] =  'z' + 1;
125             for(int j = t;j <= n;++ j) num[j] = i;
126         }
127         A.n = n; 
128         A.build_sa('z' + 2); 
129         A.build_height();
130         int l = 1, r = tmp, mid;ans = 0;
131         while(l <= r)
132         {
133             mid = (l + r) >> 1;
134             if(check(mid)) l = mid + 1, ans = mid;
135             else r = mid - 1;
136         }
137         if(ans)    put();
138         else printf("?\n");
139         putchar('\n');
140     }
141     return 0;
142 }
UVA11107

 

 

 

posted @ 2018-01-23 14:17  嘒彼小星  阅读(203)  评论(0编辑  收藏  举报