zoukankan      html  css  js  c++  java
  • 字符串(后缀数组):POJ 3294 Life Forms

    Life Forms

    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
    
    ?
    

       注意一些细节就好。

      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 using namespace std;
      5 const int maxn=110110;
      6 char s[maxn];
      7 int r[maxn],Wa[maxn],Wb[maxn],Wv[maxn],Ws[maxn];
      8 int rank[maxn],lcp[maxn],belong[maxn],sa[maxn];
      9 bool cmp(int *p,int i,int j,int l){
     10     return p[i]==p[j]&&p[i+l]==p[j+l];
     11 }
     12 void DA(int n,int m){
     13     int i,j,p,*x=Wa,*y=Wb,*t;
     14     for(i=0;i<m;i++)Ws[i]=0;
     15     for(i=0;i<n;i++)++Ws[x[i]=r[i]];
     16     for(i=1;i<m;i++)Ws[i]+=Ws[i-1];
     17     for(i=n-1;i>=0;i--)sa[--Ws[x[i]]]=i;
     18     
     19     for(j=1,p=1;p<n;m=p,j<<=1){
     20         for(p=0,i=n-j;i<n;i++)y[p++]=i;
     21         for(i=0;i<n;i++)
     22             if(sa[i]>=j)    
     23                 y[p++]=sa[i]-j;
     24         for(i=0;i<m;i++)Ws[i]=0;
     25         for(i=0;i<n;i++)++Ws[Wv[i]=x[y[i]]];
     26         for(i=1;i<m;i++)Ws[i]+=Ws[i-1];
     27         for(i=n-1;i>=0;i--)
     28             sa[--Ws[Wv[i]]]=y[i];
     29         for(t=x,x=y,y=t,x[sa[0]]=0,i=1,p=1;i<n;i++)
     30             x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;    
     31     }    
     32 }
     33 
     34 void LCP(int n){
     35     int i,j,k=0;
     36     for(i=1;i<=n;i++)rank[sa[i]]=i;
     37     for(i=0;i<n;lcp[rank[i++]]=k)
     38         for(k?k--:k,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);    
     39 }
     40 int tot,tim,vis[maxn];
     41 bool Judge(int n,int x,int g){
     42     int tmp=0;++tim;
     43     for(int i=2;i<=n;i++){
     44         if(lcp[i]<x)
     45             tmp=0,++tim;
     46         else{
     47             if(vis[belong[sa[i]]]!=tim)
     48                 tmp++,vis[belong[sa[i]]]=tim;
     49             if(vis[belong[sa[i-1]]]!=tim)
     50                 tmp++,vis[belong[sa[i-1]]]=tim;
     51             if(tmp>g)return true;
     52         }
     53     }
     54     return false;
     55 }
     56 
     57 void Solve(int n,int x,int g){
     58     int tmp=0,tag=0;++tim;
     59     for(int i=2;i<=n;i++){
     60         if(lcp[i]<x)
     61             tmp=0,++tim,tag=0;
     62         else{
     63             if(vis[belong[sa[i]]]!=tim)
     64                 tmp++,vis[belong[sa[i]]]=tim;
     65             if(vis[belong[sa[i-1]]]!=tim)
     66                 tmp++,vis[belong[sa[i-1]]]=tim;
     67             if(tmp>g&&!tag){
     68                 for(int j=0;j<x;j++)
     69                     printf("%c",r[sa[i-1]+j]);
     70                 printf("
    ");
     71                 tag=1;
     72             }
     73         }
     74     }
     75     return;
     76 }
     77 
     78 int main(){
     79     while(~scanf("%d",&tot)&&tot){
     80          
     81         int len=0,lo=1,hi=213333333;
     82         for(int i=1;i<=tot;i++){
     83             scanf("%s",s);
     84             for(int j=0;s[j];j++){
     85                 belong[len]=i;
     86                 r[len++]=s[j];
     87                 if(!s[j+1])hi=min(hi,j+1);
     88             }
     89             belong[len]=110+i;
     90             r[len++]='z'+i;    
     91         }
     92         r[len]=0; 
     93         DA(len+1,1024);
     94         LCP(len);
     95         
     96         while(lo<=hi){
     97             int mid=(lo+hi)>>1;
     98             if(Judge(len,mid,tot/2))lo=mid+1;
     99             else hi=mid-1;
    100         }
    101         if(hi>=1)
    102             Solve(len,hi,tot/2);
    103         else
    104             printf("?
    ");
    105         printf("
    ");
    106     }
    107     return 0;
    108 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    spring-cloud 注册中心配置
    SpringMVC执行流程和原理
    ssm 框架整合 配置
    Spring整合ActiveMQ 之 ActiveMQ配置
    redis配置代码
    world
    web服务器和web应用服务器的区别?
    经典面试题:Mybatis原理
    springmvc原理|执行过程|解决了什么问题?
    Hadoop 学习目录(搁置)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5370559.html
Copyright © 2011-2022 走看看