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

    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

    3
    abcdefg
    bcdefgh
    cdefghi
    3
    xxx
    yyy
    zzz
    0

    Sample Output

    bcdefg
    cdefgh
    
    ?
    

    Source

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

    #include<cstdio>//最长公共子串
    #include<cstring>
    #define Max(a, b)   a>b?a:b
    const 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<m; i++) ws[i] = 0 ;
        for(i=0; i<n; i++) ws[x[i]=r[i]] ++ ;
        for(i=1; i<m; i++) ws[i] += ws[i-1] ;
        for(i=n-1; i>=0; i--) sa[--ws[x[i]]] = i ;
        for(j=1,p=1; p<n; j*=2, m=p){
            for(p=0,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<n; i++) wv[i] = x[y[i]] ;
            for(i=0; i<m; i++) ws[i] = 0 ;
            for(i=0; i<n; i++) ws[wv[i]] ++ ;
            for(i=1; i<m; i++) ws[i] += ws[i-1] ;
            for(i=n-1; 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; i++)
            x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p - 1 : p ++ ;
        }
        return ;
    }
    void calheight(int *r, int n){
        int i, j, k = 0 ;
        for(i=1; i<=n; i++) rank[sa[i]] = i ;//获取rank值 O(n)
        for(i=0; i<n; height[rank[i++]]=k)
            for(k?k--:0, j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++) ;
        return ;
    }
    int check(int mid, int len){
        int count = 0 ;
        memset(vis, false, sizeof(vis)) ;
        for(int i=2; i<=len; i++){
            if(height[i]<mid){
                memset(vis, false, sizeof(vis)) ;
                count = 0 ;
                continue ;
            }    
            if(!vis[loc[sa[i-1]]])  vis[loc[sa[i-1]]] = true, count ++ ;
            if(!vis[loc[sa[i]]])    vis[loc[sa[i]]] = true, count ++ ;
            if(count>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]<mid){
                memset(vis, false, sizeof(vis)) ;
                count = 0 ;
                tag = 0 ;
                continue ;
            }    
            if(!vis[loc[sa[i-1]]])  vis[loc[sa[i-1]]] = true, count ++ ;
            if(!vis[loc[sa[i]]])    vis[loc[sa[i]]] = true, count ++ ;
            if(count>n/2&&!tag){
                for(int j=0; j<mid; j++)
                    ans[j] = s[sa[i]+j] + 'a' - 1 ;
                ans[mid] = '\0' ;
                printf("%s\n", ans) ;
                tag = 1 ;
            }
        }
    }
    int main(){
        int i, j, m, max, t, flag, x, temp, min ;
        while(~scanf("%d", &n)&&n){
            flag = 0 ;
            m = 0 ;
            min = maxn ;
            temp = 30 ;
            for(i=0; i<n; i++){
                scanf("%s", str) ;
                if(strlen(str)<min)    min = strlen(str) ;
                for(j=0; str[j]; j++){
                    s[m] = str[j] - 'a' + 1 ;
                    loc[m++] = i ;
                }
                s[m] = temp ;
                loc[m++] = temp ++ ;
            }
            s[m] = 0 ;
            da(s, m+1, temp) ;
            calheight(s, m) ;
            int left=0, right=min, mid ;
            while(right>=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<cstdio>
    #include<cstring>
    
    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<m;i++) ws[i]=0;
        for(i=0;i<n;i++) ws[x[i]]=r[i]++;
        for(i=1;i<m;i++) ws[i]+=ws[i-1];
        for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
    
        for(j=1,p=1;p<n;j*=2,m=p){
            for(p=0,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<n;i++) wv[i]=x[y[i]];
            for(i=0;i<m;i++) ws[i]=0;
            for(i=0;i<n;i++) ws[wv[i]]++;
            for(i=1;i<m;i++) ws[i]+=ws[i-1];
            for(i=n-1;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;i++)
                x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
    }
    
    void calheight(int *r,int n){
        int i,j,k=0;
        for(i=1;i<=n;i++)
            rank[sa[i]]=i;
        for(i=0;i<n;height[rank[i++]]=k)
            for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++) ;
    }
    
    int check(int mid,int len){
        int count=0;
        memset(vis,0,sizeof(vis));
        for(int i=2;i<=len;i++){
            if(height[i]<mid){
                memset(vis,0,sizeof(vis));
                count=0;
                continue;
            }
            if(!vis[loc[sa[i-1]]])
                vis[loc[sa[i-1]]]=true,count++;
            if(!vis[loc[sa[i]]])
                vis[loc[sa[i]]]=true,count++;
            if(count>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]<mid){
                memset(vis,0,sizeof(vis));
                count=0;
                tag=0;
                continue;
            }
            if(!vis[loc[sa[i-1]]])
                vis[loc[sa[i-1]]]=true,count++;
            if(!vis[loc[sa[i]]])
                vis[loc[sa[i]]]=true,count++;
            if(count>n/2 && !tag){
                for(int j=0;j<mid;j++)
                    ans[j]=s[sa[i]+j]+'a'-1;
                ans[mid]='\0';
                printf("%s\n",ans);
                tag=1;
            }
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int i,j,m,flag,temp,min;
        while(~scanf("%d",&n) && n){
            flag=0;
            m=0;
            min=maxn;
            temp=30;
            for(i=0;i<n;i++){
                scanf("%s",str);
                if(strlen(str)<min)
                    min=strlen(str);
                for(j=0;str[j]!='\0';j++){
                    s[m]=str[j]-'a'+1;
                    loc[m++]=i;
                }
                s[m]=temp;
                loc[m++]=temp++;
            }
            s[m]=0;
            da(s,m+1,temp);
            calheight(s,m);
            int left=0,right=min,mid;
            while(right>=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;
    }
  • 相关阅读:
    C#操作ini配置文件和写入日志操作
    asp.net AJAX 定期刷新页面,然后,在 Timer 的事件中弹出窗口
    setInterval和setTimeout的区别
    检测远程URL是否存在
    SharePoint列表的模板类型中的BaseType参数和ListTemplate参数
    TCP中的Flag options
    jQuery基础教程摘录 Hello world
    SharePoint站点无法打开的问题
    SPQuery在引用field的时候要用internal name
    Windows Server 2008中用管理员的权限使用命令行来打开程序
  • 原文地址:https://www.cnblogs.com/jackge/p/3029416.html
Copyright © 2011-2022 走看看