博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ - 3294 Life Forms
阅读量:4693 次
发布时间:2019-06-09

本文共 3816 字,大约阅读时间需要 12 分钟。

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

3abcdefgbcdefghcdefghi3xxxyyyzzz0

题意:求不小于 k 个字符串中的最长子串。

思路:将 n 个字符串连起来,中间用不同样的且没有出如今字符串中的字符隔开,求后缀数组。然后二分答案,将后缀分成若干组。推断每组的后缀是否出如今不小于 k 个的原串中。

分两次计算。第一次二分答案,第二次输出。

#include 
#include
#include
#include
#include
#include
typedef long long ll;using namespace std;const int maxn = 400005;int sa[maxn]; int t1[maxn], t2[maxn], c[maxn];int rank[maxn], height[maxn];void build_sa(int s[], int n, int m) { int i, j, p, *x = t1, *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 (j = 1; j <= n; j <<= 1) { p = 0; for (i = n-j; i < n; i++) y[p++] = i; for (i = 0; i < n; i++) if (sa[i] >= j) y[p++] = sa[i] - j; for (i = 0; i < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[y[i]]]++; for (i = 1; 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]+j] == y[sa[i]+j] ? p-1 : p++; if (p >= n) break; m = p; }}void getHeight(int s[],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--; j = sa[rank[i]-1]; while (s[i+k] == s[j+k]) k++; height[rank[i]] = k; }}int k, in[maxn], r[maxn], l[maxn];char str[maxn];int check(int mid, int n, int out) { int vis[105]; int cnt = 0; memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { if (height[i] < mid) { if (out && cnt > k / 2) { for (int j = 0, m = sa[i-1]; j < mid; j++) printf("%c", r[j+m]); printf("\n"); } cnt = 0; memset(vis, 0, sizeof(vis)); } else { if (!vis[in[sa[i-1]]]) { vis[in[sa[i-1]]] = 1; cnt++; } if (!vis[in[sa[i]]]) { vis[in[sa[i]]] = 1; cnt++; } if (!out && cnt > k / 2) return 1; } } return 0;}int main() { int first = 0; while (scanf("%d", &k) != EOF && k) { int n = 0; if (first) printf("\n"); else first = 1; for (int i = 0; i < k; i++) { scanf("%s", str); l[i] = strlen(str); for (int j = n; j < n+l[i]; j++) { r[j] = str[j-n]; in[j] = i; } n += l[i] + 1; r[n-1] = 128 + i; } n--; r[n] = 0; build_sa(r, n+1, 300); getHeight(r, n); int left = 0, right = 1000, mid, ans = -1; while (left <= right) { mid = (left + right) / 2; if (check(mid, n, 0)) { ans = mid; left = mid + 1; } else right = mid - 1; } if (ans <= 0) printf("?

\n"

); else check(ans, n, 1); } return 0; }

转载于:https://www.cnblogs.com/yangykaifa/p/6927544.html

你可能感兴趣的文章
linux安装图形界面
查看>>
博弈论之入门小结
查看>>
解决IE8下opacity属性失效问题,无法隐藏元素
查看>>
批处理文件中的路径问题
查看>>
hibernate出现No row with the given identifier exists问题
查看>>
为什么wait()和notify()属于Object类
查看>>
PHP 在5.1.* 和5.2.*之间 PDO数据库操作中的不同!
查看>>
导入properties时的坑
查看>>
配置NRPE的通讯
查看>>
shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
查看>>
匹配两个空格之间的字符。。。
查看>>
CSS 文字溢出 变成省略号 ...
查看>>
Spring事务
查看>>
java编程基础(三)流程控制语句
查看>>
让数据库跑的更快的7个MySQL优化建议
查看>>
jquery 取id模糊查询
查看>>
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>
修改node节点名称
查看>>
PAT(B) 1014 福尔摩斯的约会(Java)
查看>>
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
查看>>