hdu2328 Corporate Identity 扩展KMP

本文深入探讨了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\n");
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

 

转载于:https://www.cnblogs.com/cenariusxz/p/6592375.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值