zoukankan      html  css  js  c++  java
  • Life Forms (poj3294 后缀数组求 不小于k个字符串中的最长子串)

    (累了,这题做了很久!)
    Life Forms
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 8683   Accepted: 2375

    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 <stdio.h>
      3 #include <math.h>
      4 #include <vector>
      5 #include <string.h>
      6 using namespace std;
      7 #define N 101000
      8 vector<int> ai;
      9 int a[N],c[N],d[N],e[N],sa[N],height[N],n,b[N],m,t,jilu[110],bi[110];
     10 int cmp(int *r,int a,int b,int l)
     11 {
     12     return r[a]==r[b]&&r[a+l]==r[b+l];
     13 }
     14 void da()
     15 {
     16     int i,j,p,*x=c,*y=d,*t;
     17     memset(b,0,sizeof(b));
     18     for(i=0; i<n; i++)b[x[i]=a[i]]++;
     19     for(i=1; i<m; i++)b[i]+=b[i-1];
     20     for(i=n-1; i>=0; i--)sa[--b[x[i]]]=i;
     21     for(j=1,p=1; p<n; j*=2,m=p)
     22     {
     23         for(p=0,i=n-j; i<n; i++)y[p++]=i;
     24         for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
     25         for(i=0; i<n; i++)e[i]=x[y[i]];
     26         for(i=0; i<m; i++)b[i]=0;
     27         for(i=0; i<n; i++)b[e[i]]++;
     28         for(i=1; i<m; i++)b[i]+=b[i-1];
     29         for(i=n-1; i>=0; i--)sa[--b[e[i]]]=y[i];
     30         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
     31             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
     32     }
     33 }
     34 void callheight()
     35 {
     36     int i,j,k=0;
     37     b[0]=0;
     38     for(i=1; i<n; i++)b[sa[i]]=i;
     39     for(i=0; i<n-1; height[b[i++]]=k)
     40         for(k?k--:0,j=sa[b[i]-1]; a[i+k]==a[j+k]; k++);
     41 }
     42 int fun(int x)
     43 {
     44     int i;
     45     for(i=0; i<t; i++)
     46     {
     47         if(jilu[i]>=x)break;
     48     }
     49     return i;
     50 }
     51 bool check(int mid,int y)
     52 {
     53     int i=0;
     54     while(1)
     55     {
     56         while(i<n&&height[i]<mid)i++;
     57         if(i==n)break;
     58         memset(bi,0,sizeof(bi));
     59         int u=fun(sa[i-1]),uu=sa[i-1];
     60         bi[u]=1;
     61         int sum=1;
     62         while(i<n&&height[i]>=mid)
     63         {
     64             int yu=fun(sa[i]);
     65             if(yu==u)
     66             {
     67                 i++;
     68                 continue;
     69             }
     70             else if(!bi[yu])
     71             {
     72                 bi[yu]=1;
     73                 sum++;
     74                 u=yu;
     75             }
     76             i++;
     77         }
     78         if(sum>t/2)
     79         {
     80             if(y)
     81             ai.push_back(uu);
     82             else
     83             return 1;
     84         }
     85     }
     86     return 0;
     87 }
     88 void check1(int mid)
     89 {
     90     int i=1;
     91     while(1)
     92     {
     93         while(i<n&&height[i]<mid)i++;
     94         if(i==n)break;
     95         memset(bi,0,sizeof(bi));
     96         int u=fun(sa[i-1]),uu=sa[i-1];
     97         bi[u]=1;
     98         int sum=1;
     99         while(i<n&&height[i]>=mid)
    100         {
    101             int yu=fun(sa[i]);
    102             if(yu==u)
    103             {
    104                 i++;
    105                 continue;
    106             }
    107             else if(!bi[yu])
    108             {
    109                 bi[yu]=1;
    110                 sum++;
    111                 u=yu;
    112             }
    113             i++;
    114         }
    115         if(sum>t/2)ai.push_back(uu);
    116     }
    117 }
    118 int main()
    119 {
    120     int i,j,temp,ll=0;
    121     char x;
    122     while(scanf("%d",&t)&&t)
    123     {
    124         ai.clear();
    125         if(ll)printf("
    ");
    126         ll++;
    127         n=0;
    128         temp=30;
    129         x=getchar();
    130         for(i=0; i<t; i++)
    131         {
    132             while(x=getchar())
    133             {
    134                 if(x=='
    ')break;
    135                 a[n++]=x-'a'+1;
    136             }
    137             jilu[i]=n-1;
    138             a[n++]=temp++;
    139         }
    140         m=150;
    141         a[n-1]=0;
    142         da();
    143         callheight();
    144         int l=0,r=1001;
    145         while(l<=r)
    146         {
    147             int mid=(l+r)>>1;
    148             if(check(mid,0))
    149                 l=mid+1;
    150             else r=mid-1;
    151         }
    152         if(r)
    153         {
    154             check(r,1);
    155             for(j=0; j<ai.size(); j++)
    156             {
    157                 for(i=0; i<r; i++)
    158                 {
    159                     putchar('a'+a[ai[j]+i]-1);
    160                 }
    161                 printf("
    ");
    162             }
    163         }
    164         else
    165         {
    166             printf("?
    ");
    167         }
    168     }
    169 }
    View Code
  • 相关阅读:
    [P4721] 【模板】分治 FFT
    [GYM102452E] Erasing Numbers
    [LOJ6220] sum
    [CF776B] Sherlock and His Girlfriend
    [LOJ6087] 毒瘤题
    [LOJ2612] 花匠
    [LOJ529] 自然语言
    [CTSC2017] 吉夫特
    [LOJ6671] EntropyIncreaser 与 Minecraft
    [LOJ3196] 挂架
  • 原文地址:https://www.cnblogs.com/ERKE/p/3599484.html
Copyright © 2011-2022 走看看