poj 3450 Corporate Identity (KMP+最长公共子串)
来源:互联网 发布:网络手游赚钱排行榜 编辑:程序博客网 时间:2024/06/05 03:27
题目链接: poj 3450
题目大意: 给定n个字符串,找出最长的公共子串,若长度小于3输出IDENTITY LOST
解题思路: 选出长度最短的字符串,枚举它的子串
把它的子串分别与其余的n-1个字符串匹配
字符串长度越短它的子串就越少,枚举量越少
枚举子串的顺序从大到小,能够匹配就退出输出答案
如果从小到大枚举则需要枚举所有符合情况的子串,耗费时间
代码:
//Final 找最长的公共子串,注意 less than 3,是小于3,不等于#include <stdio.h>#include <algorithm>#include <string.h>#include <stdlib.h>#define Max 4010#define INF 0x3f3f3f3fusing namespace std;int Long[Max],Next[202];char ch[Max][202];struct snode{char ch3[202];}chtemp[Max];bool cmp(struct snode a,struct snode b){int temp=strcmp(a.ch3,b.ch3);if(temp>=0)return 0;return 1;}void Get_next(char ch1[],int Tlen){int i=0,j=-1;Next[0]=-1;while(i<Tlen){if(j==-1||ch1[j]==ch1[i]){i++; j++;Next[i]=j;}elsej=Next[j];}}int Kmp(char ch1[],int ii,int Tlen){int i=-1,j=-1;while(i<Long[ii]&&j<Tlen){if(j==-1||ch1[j]==ch[ii][i]){i++; j++;}elsej=Next[j];}if(j>=Tlen)return 1;return 0;}int main(){char ch1[202];int min,mini,i,j,j1,j2,temp,n,m,k,pd,pdi,End;while(scanf("%d",&n)!=EOF&&n){min=INF;memset(ch,0,sizeof(ch));for(i=1;i<=n;i++){scanf("%s",ch[i]);temp=strlen(ch[i]);Long[i]=temp;if(temp<min)min=temp,mini=i;}for(i=Long[mini],pd=0,m=0;i>=1;i--){End=Long[mini]-i;for(j=0;j<=End;j++){memset(ch1,0,sizeof(ch1));for(j1=j,j2=0;j2<i;j2++,j1++){ch1[j2]=ch[mini][j1];}ch1[j2]='\0';memset(Next,0,sizeof(Next));Get_next(ch1,i);for(j1=1,k=1;j1<=n;j1++){if(j1==mini)continue;if(Kmp(ch1,j1,i)){k++;}elsebreak;}if(k==n){strcpy(chtemp[m++].ch3,ch1);pd=1;pdi=i;}}if(pd==1){sort(chtemp,chtemp+m,cmp); printf("%s\n",chtemp[0].ch3);break;}}if(pd==0)printf("IDENTITY LOST\n");}return 0;}
- poj 3450 Corporate Identity (KMP+最长公共子串)
- POJ 3450 Corporate Identity(KMP:最长连续公共子序列)
- POJ 3450 Corporate Identity(kmp求多个字符串的最长公共子串)
- poj 3450 Corporate Identity 【KMP】【求公共子串】
- POJ3450 Corporate Identity(kmp,最长公共子串)
- POJ3450 Corporate Identity 最长公共子串
- POJ 3450 Corporate Identity 求所有字符的最长公共子串
- POJ-3450-Corporate Identity(KMP)
- Poj 3080 Blue Jeans + Hdu 2328 Corporate Identity (后缀数组 字典序最小的最长公共子串)
- POJ 3450--->Corporate Identity(后缀数组求多个字符串的公共子串)
- [KMP或者暴力]POJ 3450 Corporate Identity
- POJ 3450 Corporate Identity 暴力枚举+KMP
- POJ 3450Corporate Identity(暴力枚举+KMP)
- POJ 3450 Corporate Identity KMP题解
- poj 3450 Corporate Identity(数据结构:KMP)
- poj 3450 Corporate Identity 【暴力KMP】
- POJ 3450--Corporate Identity【KMP && 枚举】
- 【poj 3450 Corporate Identity 】 KMP(暴力)
- 电路,报文,分组交换等的比较
- org.apache.cxf.interceptor.Fault:Could not send Message
- CopyUtils执行从read到write的数据拷贝
- 用SHELL 查看spid执行SQL
- he Struts dispatcher cannot be found. This is usually caused by using Struts tags without the assoc
- poj 3450 Corporate Identity (KMP+最长公共子串)
- Machine Learning 书单
- Scrapy-redis分析
- iOS
- start learnning OpenGL
- [转]浅谈架构师的职责
- Log4J日志管理类使用详解
- 直接拿来用!10款实用Android UI工具
- IDEA 快捷键使用