zoukankan      html  css  js  c++  java
  • hdu2328 Corporate Identity 扩展KMP

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.

    After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.

    Your task is to find such a sequence.

    扩展KMP裸题

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<iostream>
     4 #include<string>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 const int maxn=205;
     9 int nxt[4005][maxn],ext[4005][maxn];
    10 char s[4005][maxn];
    11 int len[4005],ans[maxn];
    12 
    13 void EKMP(char s[],char t[],int lens,int lent,int c){
    14     int i,j,p,l,k;
    15     nxt[c][0]=lent;j=0;
    16     while(j+1<lent&&t[j]==t[j+1])j++;
    17     nxt[c][1]=j;
    18     k=1;
    19     for(i=2;i<lent;i++){
    20         p=nxt[c][k]+k-1;
    21         l=nxt[c][i-k];
    22         if(i+l<p+1)nxt[c][i]=l;
    23         else{
    24             j=max(0,p-i+1);
    25             while(i+j<lent&&t[i+j]==t[j])j++;
    26             nxt[c][i]=j;
    27             k=i;
    28         }
    29     }
    30 
    31     j=0;
    32     while(j<lens&&j<lent&&s[j]==t[j])j++;
    33     ext[c][0]=j;k=0;
    34     for(i=1;i<lens;i++){
    35         p=ext[c][k]+k-1;
    36         l=nxt[c][i-k];
    37         if(l+i<p+1)ext[c][i]=l;
    38         else{
    39             j=max(0,p-i+1);
    40             while(i+j<lens&&j<lent&&s[i+j]==t[j])j++;
    41             ext[c][i]=j;
    42             k=i;
    43         }
    44     }
    45 }
    46 
    47 int main(){
    48     int n;
    49     while(scanf("%d",&n)!=EOF&&n){
    50         memset(ans,0x3f,sizeof(ans));
    51         for(int i=1;i<=n;++i)scanf("%s",s[i]);
    52         for(int i=1;i<=n;++i)len[i]=strlen(s[i]);
    53         int maxx=0;
    54         for(int i=0;i<len[1];++i){
    55             for(int j=2;j<=n;++j){
    56                 int cnt=0;
    57                 EKMP(s[j],s[1]+i,len[j],len[1]-i,j);
    58                 for(int k=0;k<len[j];++k){
    59                     if(ext[j][k]>cnt)cnt=ext[j][k];
    60                 }
    61                 if(cnt<ans[i])ans[i]=cnt;
    62             }
    63             if(ans[i]>maxx)maxx=ans[i];
    64         }
    65         if(!maxx)printf("IDENTITY LOST
    ");
    66         else{
    67             string str[205];
    68             int cnt=0;
    69             for(int i=0;i<len[1];++i)if(ans[i]==maxx){
    70                 str[++cnt]=string(s[1]+i,ans[i]);
    71             }
    72             sort(str+1,str+cnt+1);
    73             cout<<str[1]<<endl;
    74         }
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    高维协方差矩阵估计
    互信息
    投资组合模型
    R语言
    sklearn
    Python学习
    swagger使用过程中遇到的坑
    mysql杂文
    2018狗年,半年报
    Springboot 手动搭建项目 --redis配置&日志完善+用户名
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592375.html
Copyright © 2011-2022 走看看