博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3294 Life Forms (后缀数组)
阅读量:6679 次
发布时间:2019-06-25

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

Life Forms
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7270   Accepted: 1998

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

Sample Output

bcdefgcdefgh?

Source

, 2006.9.30

 

 

依旧二分判定,只是要先找出最大长度,然后在根据这个长度再判定输出。对于长度相同的,用tag标记以免重复。

 

#include
//最长公共子串#include
#define Max(a, b) a>b?a:bconst int maxn = 101001 ;int wa[maxn], wb[maxn], wv[maxn], ws[maxn], rank[maxn], height[maxn], sa[maxn], s[maxn], loc[maxn] ;bool vis[1001] ;char str[1001], ans[1001] ;int n ;int cmp(int *r,int a,int b,int l){ return r[a]==r[b] && r[a+l]==r[b+l] ;}int abs(int a){ return a<0?-a:a ;}void da(int *r, int n, int m){ int i, j, p, *x = wa, *y = wb, *t ; for(i=0; i
=0; i--) sa[--ws[x[i]]] = i ; for(j=1,p=1; p
=j) y[p++] = sa[i] - j ; for(i=0; i
=0; i--) sa[--ws[wv[i]]] = y[i] ; for(t=x, x=y, y=t, p=1, x[sa[0]]=0, i=1; i
n/2) return 1 ; } return 0 ;}void print(int mid, int len){ int count = 0, tag = 0 ; memset(vis, false, sizeof(vis)) ; for(int i=2; i<=len; i++){ if(height[i]
n/2&&!tag){ for(int j=0; j
=left){ mid = (right + left) / 2 ; if(check(mid, m)){ left = mid + 1 ; flag = mid ; } else right = mid - 1 ; } if(flag){ print(flag, m) ; printf("\n") ; } else printf("?\n\n") ; } return 0 ;}

 

 

下面我的代码 RE

 

#include
#include
const int maxn=111111;int wa[maxn],wb[maxn],wv[maxn],ws[maxn];int rank[maxn],height[maxn],sa[maxn],s[maxn],loc[maxn];bool vis[1010];char str[1010],ans[1010];int n;int cmp(int *r,int a,int b,int l){ return r[a]==r[b] && r[a+l]==r[b+l];}void da(int *r,int n,int m){ int i,j,p,*x=wa,*y=wb,*t; for(i=0;i
=0;i--) sa[--ws[x[i]]]=i; for(j=1,p=1;p
=j) y[p++]=sa[i]-j; for(i=0;i
=0;i--) sa[--ws[wv[i]]]=y[i]; for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i
n/2) return 1; } return 0;}void print(int mid,int len){ int count=0,tag=0; memset(vis,0,sizeof(vis)); for(int i=2;i<=len;i++){ if(height[i]
n/2 && !tag){ for(int j=0;j
=left){ mid=(left+right)>>1; if(check(mid,m)){ left=mid+1; flag=mid; }else right=mid-1; } if(flag){ print(flag,m); printf("\n"); }else printf("?\n\n"); } return 0;}

 

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

你可能感兴趣的文章
麒麟开源堡垒机功能版本说明及升级方式说明
查看>>
交换机SPAN功能配置
查看>>
关于ssh的问题
查看>>
作业 rh124
查看>>
Docker创建tomcat镜像
查看>>
Restful学习随笔
查看>>
2018区块链学习路线及大纲附Java,Python,初级高级,深入浅出视频教程
查看>>
[Algorithms] Longest Common Subsequence
查看>>
常见排序算法总结(含C/C++代码)
查看>>
CurrentRowColor 选中行 颜色改变
查看>>
内容溢出显示省略号
查看>>
二维码(支持arc,苹果自带扫描,zbar扫描,二维码生成)
查看>>
更改matlab默认工作路径
查看>>
[转] EM算法
查看>>
在VS中折叠所有代码或展开所有代码
查看>>
JavaScript 书籍推荐(转)
查看>>
《活法》摘录一
查看>>
C/S和B/S结构区别整理
查看>>
python基础===理解Class的一道题
查看>>
Bootstrap3 概述
查看>>