POJ - 3450
Posted caijiaming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ - 3450相关的知识,希望对你有一定的参考价值。
题目链接:http://poj.org/problem?id=3450
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8549 | Accepted: 2856 |
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
3 aabbaabb abbababb bbbbbabb 2 xyz abc 0
Sample Output
abb IDENTITY LOST
Source
#include<iostream> #include<string.h> #include<map> #include<cstdio> #include<cstring> #include<stdio.h> #include<cmath> #include<ctype.h> #include<math.h> #include<algorithm> #include<set> #include<queue> typedef long long ll; using namespace std; const ll mod=1000; const int maxn=4e3+10; const int maxk=5e3+10; const int maxx=1e4+10; const ll maxe=1000+10; #define INF 0x3f3f3f3f3f3f #define Lson l,mid,rt<<1 #define Rson mid+1,r,rt<<1|1 char a[maxn][210]; int next[maxn]; void cal_next(char s[]) { int len=strlen(s); int k=-1; next[0]=-1; for(int i=1;i<len;i++) { while(k>-1&&s[k+1]!=s[i]) { k=next[k]; } if(s[k+1]==s[i]) k++; next[i]=k; } } bool kmp(char x[],char y[]) { int k=-1; int len1=strlen(x); int len2=strlen(y); for(int i=0;i<len1;i++) { while(k>-1&&y[k+1]!=x[i]) { k=next[k]; } if(x[i]==y[k+1]) k++; if(k==len2-1) return true; } return false; } int main() { //ios::sync_with_stdio(false); int n,flag=0; while(scanf("%d",&n)!=EOF) { getchar(); if(n==0) break; char ans[210]="",temp[210]; scanf("%s",a[0]); getchar(); strcpy(temp,a[0]);//这里不用自己加‘ ‘,因为a[0]本身就有‘ ‘的,直接复制过去了 for(int i=1;i<n;i++) { scanf("%s",a[i]); getchar(); if(strlen(a[i])<strlen(temp)) { strcpy(temp,a[i]); } } int len=strlen(temp); for(int i=0;i<len;i++)//起点 { for(int j=1;i+j<=len;j++)//长度 { char op[210]; strncpy(op,temp+i,j);//不会在结尾给你自动加‘ ‘ op[j]=‘