UVA - 11107
Description ![]() ![]() Problem C: Life FormsYou 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. 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. 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 Input3 abcdefg bcdefgh cdefghi 3 xxx yyy zzz 0 Output for Sample Inputbcdefg cdefgh ? Gordon V. Cormack Source
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: String Processing :: Suffix Trie, Tree, Array ::
Standard
Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 3. Data Structures :: String Algorithms :: Examples Root :: Prominent Problemsetters :: Gordon V. Cormack Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 6. String Processing :: Suffix Array Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: String Processing :: Suffix Trie, Tree, Array - Standard |
题意:
输入n个DNA序列,你的任务是求出一个长度最大的字符串,使得它在超过一般的DNA序列中出现。如果有多解,按照字典序从小到大输入所有解。
第一次写后缀数组
把n个DNA序列拼在一起,中间用没有出现过的字符分割。然后求出height数组。
二分满足要求的字符串长度L,然后判断是否可行。
判断可行:
分组方法,如果某一组(段)有超过n/2的DNA串(是对应的输入的DNA串要有n/2个),则可行。
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));
const int maxn = 100 + 20;
const int maxl = 1000 + 20;
const int sa_maxl = maxn * maxl;
int s[sa_maxl];
int sa[sa_maxl];
int t[sa_maxl], t2[sa_maxl], c[sa_maxl];
char str[maxn][maxl];
int idx[sa_maxl];
void build_sa(int * s, int * sa, int n, int m) {
int i, *x = t, *y = t2;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[i] = s[i]]++;
for(i = 1; i < m; i++) c[i] += c[i-1];
for(i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;
for(int k = 1; k <= n; k <<= 1) {
int p = 0;
for(i = n-k; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++;
for(i = 0; i < m; i++) c[i] += c[i-1];
for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1; x[sa[0]] = 0;
for(i = 1; i < n; i++)
x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
if(p >= n) break;
m = p;
}
}
int rank[sa_maxl], height[sa_maxl];
void getHeight(int n) {
int i, j, k = 0;
for(i = 0; i < n; i++) rank[sa[i]] = i;
for(i = 0; i < n; i++) {
if(k) k--;
int j = sa[rank[i]-1];
while(s[i+k] == s[j+k]) k++;
height[rank[i]] = k;
}
}
int flag[sa_maxl];
bool good(int L, int R, int n, int limt) {
if(R - L < limt) return false;
memset(flag, 0, sizeof(flag));
int cnt = 0;
for(int i = L; i < R; i++) {
int x = idx[sa[i]];
if(x != n && !flag[x]) { flag[x] = 1; cnt++; }
}
return cnt >= limt;
}
void print_sub(int len, int L, int n) {
while(idx[L] == n) L++;
int x = sa[L];
for(int i = 0; i < len; i++) {
putchar(s[x+i] + 'a' - 1);
}
putchar('\n');
}
bool judge(int len, int n, int limt, bool print) {
int L = 0;
for(int R = 1; R <= n; R++) {
if(R == n || height[R] < len) {
if(good(L, R, n, limt)) {
if(print) { print_sub(len, L, n); } else return true;
}
L = R;
}
}
return false;
}
int main() {
int n;
int kase = 1;
while(scanf("%d", &n) != EOF && n) {
if(kase++ > 1) putchar('\n');
int len = 0;
int maxlen = 0;
for(int i=0; i<n; i++) {
scanf("%s", str[i]);
int sz = strlen(str[i]);
maxlen = max(maxlen, sz);
for(int j=0; j<sz; j++) {
s[len] = str[i][j] - 'a' + 1;
idx[len++] = i;
}
s[len] = 27 + i;
idx[len++] = n;
}
s[len] = 0;
idx[len++] = n;
int m = 27 + n;
if(n == 1) {
puts(str[0]);
continue;
}
int limt = n / 2 + 1;
build_sa(s, sa, len, m);
getHeight(len);
if(!judge(1, len, limt, false)) {
puts("?");
continue;
}
int L = 1, R = maxlen;
while(L < R) {
int M = L + (R-L+1) / 2;
if(judge(M, len, limt, false)) L = M;
else R = M - 1;
}
judge(L, len, limt, true);
}
return 0;
}