poj 3294 Life Forms

Life Forms
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 18777 Accepted: 5530

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

?

Source

 
给定n个字符串,求出现在超过n/2个字符串中的最长子串。 
将n个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开, 求后缀数组。
然后二分答案,将后缀分成若干组,判断每组的后缀是否出现在不小于k个的原串中。这个做法的时间复杂度为O(nlogn)。
 
  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<cmath>
  5 #include<algorithm> 
  6 using namespace std;  
  7 int const N=100000+1000;  
  8 int sa[N],wa[N<<1],wb[N<<1],rk[N],num[N],h[N],wv[N],vis[101],id[N],s[N];   
  9 char s1[1003]; 
 10 int cmp(int *r,int x,int y,int z){
 11     return r[x]==r[y] && r[x+z]==r[y+z];  
 12 }
 13 void build_sa(int *r,int *sa,int n,int m){ 
 14     int i,j,p,*x=wa,*y=wb;  
 15     for(i=0;i<m;i++) num[i]=0; 
 16     for(i=0;i<n;i++) num[x[i]=r[i]]++; 
 17     for(i=1;i<m;i++) num[i]+=num[i-1];  
 18     for(i=n-1;i>=0;i--) sa[--num[x[i]]]=i;
 19     for(j=1,p=1;p<n;j<<=1,m=p){
 20         for(p=0,i=n-j;i<n;i++) y[p++]=i;  
 21         for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;  
 22         for(i=0;i<m;i++) num[i]=0; 
 23         for(i=0;i<n;i++) wv[i]=x[y[i]]; 
 24         for(i=0;i<n;i++) num[wv[i]]++;  
 25         for(i=1;i<m;i++) num[i]+=num[i-1];  
 26         for(i=n-1;i>=0;i--) sa[--num[wv[i]]]=y[i];  
 27         swap(x,y);  
 28         for(i=1,p=1,x[sa[0]]=0;i<n;i++) 
 29             x[sa[i]]=cmp(y,sa[i],sa[i-1],j)? p-1: p++;  
 30     }
 31     for(i=0;i<n;i++) rk[i]=x[i]; 
 32 }
 33 void build_h(int *r,int *sa,int n){
 34     int k=0;  
 35     for(int i=0;i<n;i++){
 36         if(k) k--; 
 37         int j=sa[rk[i]-1]; 
 38         while (r[i+k]==r[j+k]) k++;  
 39         h[rk[i]]=k; 
 40     }
 41 }
 42 int check(int mid,int n,int tot){
 43     if(tot==1) return 1;  
 44     int num=0; 
 45     memset(vis,0,sizeof(vis));  
 46     for(int i=1;i<=n;i++){
 47         if(h[i]>=mid){
 48             if(!vis[id[sa[i-1]]]){
 49                 num++; vis[id[sa[i-1]]]=1; 
 50             }
 51             if(!vis[id[sa[i]]]){
 52                 num++; vis[id[sa[i]]]=1; 
 53             }
 54             if(num>tot/2) return 1; 
 55         }else {
 56             num=0;  
 57             memset(vis,0,sizeof(vis));  
 58         }
 59     }
 60     return 0; 
 61 }
 62 int solve(int mid,int n,int tot){
 63     int num=0; 
 64     memset(vis,0,sizeof(vis));  
 65     for(int i=1;i<=n;i++){
 66         if(h[i]>=mid){
 67             if(!vis[id[sa[i-1]]]){
 68                 num++; vis[id[sa[i-1]]]=1; 
 69             }
 70             if(!vis[id[sa[i]]]){
 71                 num++; vis[id[sa[i]]]=1; 
 72             }
 73         }else {
 74             if(num>tot/2) {
 75                 for(int j=sa[i-1];j<sa[i-1]+mid;j++) printf("%c",s[j]); 
 76                 printf("\n");
 77             }
 78             num=0;  
 79             memset(vis,0,sizeof(vis));  
 80         }
 81     }
 82     if(num>tot/2) {
 83         for(int j=sa[n];j<sa[n]+mid;j++) printf("%c",s[j]); 
 84         printf("\n");
 85     }
 86 }    
 87 int main(){
 88     int n; int t=0;  
 89     while (scanf("%d",&n)!=EOF && n){
 90         if(t++) printf("\n");  
 91         memset(s,0,sizeof(s));  
 92         int sum=130,len=0;  
 93         for(int i=0;i<n;i++){
 94             scanf("%s",s1);  
 95             int l=len;  
 96             int k=strlen(s1);  
 97             len+=k+1;  
 98             for(int j=l;j<len-1;j++) id[j]=i+1,s[j]=s1[j-l];  
 99             s[len-1]=sum++;  
100         }  
101         build_sa(s,sa,len+1,sum);   
102         build_h(s,sa,len); 
103         int l=0,r=1000,ans=0;  
104         while (l<=r){
105             int mid=(l+r)/2; 
106             if(check(mid,len,n)) {
107                 ans=mid;  
108                 l=mid+1; 
109             }else r=mid-1;  
110         }
111         if(ans==0) printf("?\n"); 
112         else solve(ans,len,n);  
113     }
114     return 0; 
115 }

 

转载于:https://www.cnblogs.com/ZJXXCN/p/10967167.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值