poj Corporate Identity 3450 (KMP&&枚举) 好题
来源:互联网 发布:像一世之尊的小说知乎 编辑:程序博客网 时间:2024/05/22 11:03
Description
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.
Input
The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.
After the last trademark, the next task begins. The last task is followed by a line containing zero.
Output
For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.
Sample Input
3aabbaabbabbababbbbbbbabb2xyzabc0
Sample Output
abbIDENTITY LOST
//题意:
给你n个字符串让你找出他们中的最长公共子串。如果最长公共子串有多个,则输出字典序小的那个子串。
//看了大神的代码一早上还是有点迷糊,先贴上代码。。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char res[210];char dict[4010][210];int p[210];int n;void getp(char s[],int l){memset(p,0,sizeof(p));for(int i=1,j=0;i<l;){if(s[i]==s[j]){j++;p[i]=j;i++;}else if(j>0)j=p[j-1];else i++;}}int kmp(char s[],int l){int i,j;getp(s,l);for(i=1;i<n;i++){char *q=dict[i];int j=0;int tmp=0;for(;*q&&j<l;){if(*q==s[j]){q++;j++;tmp=max(tmp,j);}else if(j>0)j=p[j-1];elseq++;}l=tmp;}return l;}int main(){while(scanf("%d",&n),n){getchar();for(int i=0;i<n;i++)gets(dict[i]);int l=strlen(dict[0]);int ans=0;int pos=0;for(int i=0;i<l;i++){int tmp=kmp(dict[0]+i,l-i);if(tmp>=ans){if(tmp>ans){ans=tmp;pos=i;}else{bool sma=true;for(int t=0;t<ans;t++){if(dict[0][pos+t]>dict[0][i+t])break;else if(dict[0][pos+t]<dict[0][i+t]){sma=false;break;}}if(sma)pos=i;}}}if(ans){for(int i=0;i<ans;i++)printf("%c",dict[0][pos+i]);printf("\n");}elseprintf("IDENTITY LOST\n");}return 0;}
- poj Corporate Identity 3450 (KMP&&枚举) 好题
- 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+暴搞)
- poj 3450 Corporate Identity(二分长度 + 暴力kmp)
- POJ 题目3450 Corporate Identity(KMP 暴力)
- POJ-3450 Corporate Identity(KMP/后缀数组)
- [KMP或者暴力]POJ 3450 Corporate Identity
- POJ 3450 Corporate Identity KMP题解
- poj 3450 Corporate Identity(数据结构:KMP)
- poj 3450 Corporate Identity 【暴力KMP】
- Corporate Identity (KMP+暴力枚举)
- poj 3450 Corporate Identity 枚举+kmp,话说这家伙给我一顿超时啊!!!!
- poj 3450 Corporate Identity
- makefile demo
- 深刻理解 js对象间的赋值
- 【鸟哥的linux私房菜-学习笔记】EXT2 文件系统
- 写给那些想做程序员和不想做程序员的人
- 《javascript语言精粹》读书笔记 Item2 对象
- poj Corporate Identity 3450 (KMP&&枚举) 好题
- 测试编辑器
- GIS学习笔记之矢量化
- 2015 China Collegiate Programming Contest
- vagrant virtualbox VM inaccessible解决办法
- 机房收费系统之用例图
- 如何写一个.properties文件
- Xcode7.1 App上线提交不了问题汇总
- LEETCODE-Rectangle Area