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 ?
跟上一道题差不多,只不过要输方案而已。
先算出公共串的长度,然后符合长度的公共串都输出就好
注意:“ output the longest string or strings shared by more than half of the life forms.” 大于一半的条件是: >(n/2)!#include <iostream> #include <stdio.h> #include <string> #include <cstring> #define N 2000010 long r[N] = {0}, x[N] = {0}, sa[N] = {0}, b[N] = {0}; long wa[N] = {0}, wb[N] = {0}, ws[N] = {0}, wv[N] = {0}; long rank[N] = {0}, height[N] = {0}; long n = 0, len = 0, z = 0; bool cmp (long *r, long a, long b, long l) { return ((r[a] == r[b]) && (r[a + l] == r[b + l])); } void calcsa(long *r, long *sa, long n, long m) { long i, j, p, *x = wa, *y = wb, *t; for (i = 0; i < m; i++) ws[i] = 0; for (i = 0; i < n; i++) ws[x[i] = r[i]]++; for (i = 1; i < m; i++) ws[i] += ws[i - 1]; for (i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i; for (j = 1, p = 1; p < n; j *= 2, m = p) { for (p = 0, i = n - j; i < n; i++) y[p++] = i; for (i = 0; i < n; i++) if (sa[i] - j >= 0) y[p++] = sa[i] - j; for (i = 0; i < n; i++) wv[i] = x[y[i]]; for (i = 0; i < m; i++) ws[i] = 0; for (i = 0; i < n; i++) ws[wv[i]]++; for (i = 1; i < m; i++) ws[i] += ws[i - 1]; for (i = n - 1; i >= 0;i--) sa[--ws[wv[i]]] = y[i]; t = x; x = y; y = t; p = 1; x[sa[0]] = 0; for (i = 1; i < n ; i++) { x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } } } void calcheight(long *r, long *sa, long n) { long i, j, p; for (i = 1; i <= n; i++) { rank[sa[i]] = i; } p = 0; for (i = 0; i < n; i++) { j = sa[rank[i] - 1]; while (r[j + p] == r[i + p]) { p++; } height[rank[i]] = p; if (p > 0) p--; } } void write(long re) { long i, j, k, t, s, minn; for (i = 2; i <= len; i = j + 1) { while (height[i] < re && i <= len) i++; j = i; while (height[j] >= re) j++; if (j - i + 1 < ((n) / 2)) continue; s = 0; z++; minn = len; for (k = i - 1; k < j; k++) { if (((t = x[sa[k]]) != 0) &&(b[t] != z)) { if (sa[k] < minn) minn= sa[k]; b[t] = z; s++; } } if (s > ((n) / 2)) { for (k = minn; k < minn + re; k++) printf("%c", r[k] - 200); printf("\n"); } } } bool check(long mid) { long i, j, k, t, s; for (i = 2; i <= len; i = j + 1) { while (height[i] < mid && i <= len) i++; j = i; while (height[j] >= mid) j++; if (j - i + 1 < ((n) / 2)) continue; s = 0; z++; for (k = i - 1; k < j; k++) { if (((t = x[sa[k]]) != 0) &&(b[t] != z)) { b[t] = z; s++; } } if (s > ((n) / 2)) return 1; } return 0; } void deal() { long l = 0, r = len; while (l <= r) { long mid = (l + r) / 2; if (check(mid)) l = mid + 1; else r = mid - 1; } if (r <= 0) printf("?\n"); else{ // printf("%d\n", r); write(r); } } int main() { while ((scanf("%d", &n) != EOF) && (n != 0)) { len = 0; char c[1100]; for (long i = 1; i <= n; i++) { scanf("%s", c); long k = strlen(c); for (long j = 0; j < k; j++) { r[j + len] = c[j] + 200; x[j + len] = i; } r[k + len] = i; x[len + k] = 0; len += k + 1; } len--; r[len] = 0; if (n == 1) { for (long i = 0; i < len / 2; i++) printf("%c", r[i] - 200); printf("\n\n"); continue; } calcsa(r, sa, len + 1, 400); calcheight(r, sa, len); deal(); printf("\n"); } return 0; }