| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 11322 | Accepted: 3135 |
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, titledThe 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 ?
Source
给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串。
算法分析:
将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,
求后缀数组。然后二分答案,将后缀分成若干组,判断每组
的后缀是否出现在不小于 k 个的原串中。这个做法的时间复杂度为 O(nlogn)。
***唯一需要注意的便是如果要你按字典序上升输出,便应该以排名枚举height
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=200000+100;
int Belong[maxn];
int s[maxn];
int num,ansnum;
int ans[maxn];
bool vis[maxn];
char s1[maxn];
int sa[maxn],t[maxn],t2[maxn],c[maxn],n;
//构造字符串s的后缀数组,每个字符值必须为0~m-1
void build_sa(int m){
int *x=t,*y=t2;
//基数排序
for(int i=0;i<m;i++) c[i]=0;
for(int i=0;i<n;i++) c[x[i]=s[i]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
for(int k=1;k<=n;k<<=1){
int p=0;
//直接利用sa数组排序第二关键字
for(int i=n-k;i<n;i++) y[p++]=i;
for(int i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
//基数排序第一关键字
for(int i=0;i<m;i++) c[i]=0;
for(int i=0;i<n;i++) c[x[y[i]]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
//根据sa和y计算新的x数组
swap(x,y);
p=1;
x[sa[0]]=0;
for(int 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 rank1[maxn],height[maxn];
void getHeight(){
int i,j,k=0;
for(i=0;i<=n;i++) rank1[sa[i]]=i;
for(i=0;i<n;i++){
if(k)
k--;
int j=sa[rank1[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank1[i]]=k;
}
}
bool get_divid(int beg,int end1){
int cnt=0;
memset(vis,false,sizeof(vis));
for(int i=beg;i<=end1;i++){ //排名
if(vis[Belong[sa[i]]]==false){
cnt++;
vis[Belong[sa[i]]]=true;
if(cnt>num/2)
return true;
}
}
return false;
}
bool check(int m){
bool res=false;
int beg=0,end1=0;
for(int i=1;i<=n;i++){
while(i<n&&height[i]>=m){
i++;
end1++;
}
if(end1-beg>=num/2){
if(get_divid(beg,end1)){
if(!res){
ansnum=0;
res=true;
}
ans[ansnum++]=beg;
}
}
end1=beg=i;
}
if(end1-beg>=num/2){
if(get_divid(beg,end1)){
if(!res){
ansnum=0;
res=true;
}
ans[ansnum++]=beg;
}
}
return res;
}
int main(){
int case1=0;
while(scanf("%d",&num)!=EOF){
if(num==0)
break;
if(case1!=0)
printf("\n");
case1++;
int cnt=0;
int num_case=1;
int high=0;
for(int i=0;i<num;i++){
scanf("%s",s1);
int len=strlen(s1);
high=max(high,len);
for(int j=0;j<len;j++){
Belong[cnt]=i;
s[cnt++]=s1[j]-'a'+110;
}
if(i!=num-1)
s[cnt++]=num_case++;
}
s[cnt++]=0;
n=cnt;
build_sa(200);
n--;
getHeight();
int max_len=0;
int low=1;
ansnum=0;
while(high-low>=0){
int mid=(high+low)>>1;
if(check(mid)){
low=mid+1;
max_len=mid;
}
else
high=mid-1;
}
if(ansnum==0)
printf("?\n");
else{
for(int i=0;i<ansnum;i++){
int j=sa[ans[i]];
for(int k=0;k<max_len;k++)
printf("%c",s[j+k]-110+'a');
printf("\n");
}
}
}
return 0;
}
/*
4
acbde
sadbxcvnasjkheg
cxbnmzbcasg
twyeuxabnscmd
*/
本文深入探讨了信息技术领域的多个方面,包括前端开发、后端开发、移动开发、游戏开发、大数据开发等细分领域。从基础知识到最新趋势,全面覆盖。同时,文章还涉及开发工具、嵌入式硬件、音视频基础、测试、基础运维、DevOps、操作系统、云计算厂商、自然语言处理、区块链、隐私计算、文档协作与知识管理、版本控制、项目管理与协作工具等多个领域,提供详尽的技术解析与实践指导。
270

被折叠的 条评论
为什么被折叠?



