zoukankan      html  css  js  c++  java
  • 【POJ】3294 Life Forms

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 using namespace std;
      5 #define MAXN 1100
      6 #define MAXL 200100
      7 char str[MAXN][MAXN],s[MAXL];
      8 int wa[MAXL],wb[MAXL],wv[MAXL],ws[MAXL];
      9 int sa[MAXL],height[MAXL],rk[MAXL];
     10 int m,len[MAXN];
     11 inline bool cmp(int *r,int a,int b,int L)
     12 {
     13     return r[a]==r[b]&&r[a+L]==r[b+L];
     14 }
     15 void SA(int n,int m)
     16 {
     17     int i,j,p,*x=wa,*y=wb,*t;
     18     for(i=0;i<m;i++)
     19         ws[i]=0;
     20     for(i=0;i<n;i++)
     21         ws[x[i]=s[i]]++;
     22     for(i=1;i<m;i++)
     23         ws[i]+=ws[i-1];
     24     for(i=n-1;i>=0;i--)
     25         sa[--ws[x[i]]]=i;
     26     for(j=p=1;p<n;j<<=1,m=p)
     27     {
     28         for(p=0,i=n-j;i<n;i++)
     29             y[p++]=i;
     30         for(i=0;i<n;i++)
     31         {
     32             if(sa[i]>=j)
     33                 y[p++]=sa[i]-j;
     34         }
     35         for(i=0;i<m;i++)
     36             ws[i]=0;
     37         for(i=0;i<n;i++)
     38             ws[wv[i]=x[y[i]]]++;
     39         for(i=1;i<m;i++)
     40             ws[i]+=ws[i-1];
     41         for(i=n-1;i>=0;i--)
     42             sa[--ws[wv[i]]]=y[i];
     43         for(t=x,x=y,y=t,x[sa[0]]=0,p=i=1;i<n;i++)
     44             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
     45     }
     46 }
     47 void Height(int n)
     48 {
     49     int i,j,k;
     50     for(i=1;i<=n;i++)
     51         rk[sa[i]]=i;
     52     for(i=k=0;i<n;height[rk[i++]]=k)
     53         for(k?k--:0,j=sa[rk[i]-1];s[i+k]==s[j+k];k++);
     54 }
     55 bool OK(int n,int mid)
     56 {
     57     int i,j,k,t,cnt;
     58     bool h[MAXN];
     59     for(i=1;i<=n;i++)
     60     {
     61         if(height[i]>=mid)
     62         {
     63             memset(h,false,sizeof(h));
     64             for(j=i;j<=n&&height[j]>=mid;j++);
     65             for(k=i-1;k<j;k++)
     66             {
     67                 t=upper_bound(len,len+m,sa[k])-len;
     68                 h[t]=true;
     69             }
     70             for(k=cnt=0;k<m;k++)
     71             {
     72                 if(h[k])
     73                     cnt++;
     74             }
     75             if(cnt>(m>>1))
     76                 return true;
     77             i=j-1;
     78         }
     79     }
     80     return false;
     81 }
     82 void Print(int n,int low)
     83 {
     84     int i,j,k,t,cnt;
     85     bool h[MAXN];
     86     for(i=1;i<=n;i++)
     87     {
     88         if(height[i]>=low)
     89         {
     90             memset(h,false,sizeof(h));
     91             for(j=i;j<=n&&height[j]>=low;j++);
     92             for(k=i-1;k<j;k++)
     93             {
     94                 t=upper_bound(len,len+m,sa[k])-len;
     95                 h[t]=true;
     96             }
     97             for(k=cnt=0;k<m;k++)
     98             {
     99                 if(h[k])
    100                     cnt++;
    101             }
    102             if(cnt>(m>>1))
    103             {
    104                 for(k=sa[i];k<sa[i]+low;k++)
    105                     putchar(s[k]);
    106                 putchar('\n');
    107             }
    108             i=j-1;
    109         }
    110     }
    111 }
    112 int main()
    113 {
    114     int n,i,j,high,low,mid;
    115     bool first=true;
    116     while(scanf("%d",&m),m)
    117     {
    118         if(first)
    119             first=false;
    120         else
    121             putchar('\n');
    122         high=0;
    123         for(i=s[0]=0;i<m;i++)
    124         {
    125             scanf(" %s",str[i]);
    126             n=strlen(str[i]);
    127             if(i+1>='a')
    128                 str[i][n]='a'-1;
    129             else
    130                 str[i][n]=i+1;
    131             str[i][n+1]=0;
    132             high=max(high,n);
    133             strcat(s,str[i]);
    134             len[i]=strlen(s);
    135         }
    136         n=len[m-1]-1;
    137         s[n]=0;
    138         SA(n+1,256);
    139         Height(n);
    140         for(low=0,high++;low<high;)
    141         {
    142             mid=(low+high)>>1;
    143             if(OK(n,mid))
    144                 low=mid+1;
    145             else
    146                 high=mid;
    147         }
    148         low=max(low-1,0);
    149         if(low)
    150             Print(n,low);
    151         else
    152             puts("?");
    153     }
    154     return 0;
    155 }
  • 相关阅读:
    MVC中CheckBox
    Python中的高级数据结构
    高级正则表达式技术(Python版)
    程序员可以兼任项目经理吗?
    浅谈五大Python Web框架
    学习Python编程的11个资源
    Python 代码性能优化技巧
    python多线程ctrl-c退出问题
    Python 笔记 : 类和继承
    Python的OO思想
  • 原文地址:https://www.cnblogs.com/DrunBee/p/2585830.html
Copyright © 2011-2022 走看看