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

本文共 3762 字,大约阅读时间需要 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; }

转载地址:http://thivx.baihongyu.com/

你可能感兴趣的文章
我的Android开发之路——百度地图开源工具获取定位信息
查看>>
Journal List
查看>>
Zookeeper(详)
查看>>
定位流
查看>>
MySql-8.0.12 安装教程随笔
查看>>
os模块
查看>>
vue实战记录(一)- vue实现购物车功能之前提准备
查看>>
51nod 1101 换零钱 【完全背包变形/无限件可取】
查看>>
回溯法练习【BFS/DFS】
查看>>
Python-CSS 基础
查看>>
正则基础之——贪婪与非贪婪模式
查看>>
Flask中路由模块的实现
查看>>
修改的样式没有更新
查看>>
转载:python异常之 GeneratorExit
查看>>
Avogadro
查看>>
[HDU1890]RoboticSort
查看>>
升学——妹妹小鱼儿升学所让我想到的
查看>>
怎样面试产品经理
查看>>
bootstrap 对话框调用 iframe
查看>>
grep, sed 与 awk 补补课,到底怎么用!
查看>>