Corporate Identity
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 9460 | Accepted: 3122 |
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
这题我感觉和POJ - 3080 Blue Jeans差不多,我用的代码其实就是在3080这题基础上给它稍微的修改了一下
https://blog.csdn.net/qq_43813140/article/details/100095344
话不多说,这题的思路:
先找到最短的那个字符串并标记,记录最短字符串的长度,开始从最大的长度到1递减,枚举出所标记字符串指定长度的子字符串,让它与其它未被标记的字符串进行匹配(用kmp判断匹配是否成功),如果在所有未标记的字符串里面都有这个子字符串那么匹配成功,并记录此时的字符串,具体细节看代码,“}”的Ascll值比z要大。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string.h>
#include<iterator>
#include<vector>
#include<stdlib.h>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<sstream>
#define lowbit(x) (x&(-x))
typedef long long ll;
using namespace std;
const int N = 4000,M = 200,L = 200;
char s[N][M],tans[M],ans[M];
bool kmp(char s[],char p[])
{
int Next[M];
Next[0] = -1,Next[1]=0;
int plen = strlen(p),i=0,j,slen = strlen(s);
while(++i<plen-1)
{
j = Next[i];
while(j!=-1&&p[i]!=p[j])
j = Next[j];
Next[i+1] = j+1;
}
i = j =0;
while(i<slen&&j<plen)
{
if(j==-1||s[i]==p[j])++i,++j;
else
j = Next[j];
}
return j==plen;
}
int main()
{
int n,flag;
while(scanf("%d",&n),n!=0)
{
flag = 0;
int mn = 300;
int pos;
for(int i=0; i<n; i++)
{
scanf("%s",s[i]);
int len = strlen(s[i]);
if(mn>len)
{
mn = len;
pos = i;
}
}
for(int lans = mn; lans>=1&&flag==0; --lans)
{
//printf("%d\n",lans);
strcpy(ans,"}");
for(int i=0,j; i+lans<=mn; ++i)
{
//printf("%d ",i);
for(j = 0; j<lans; ++j)
tans[j] = s[pos][i+j];
//printf("%s\n",tans);
tans[j] = '\0';
int f1=0;
for(int k=0;k<n;k++)
{
if(k==pos)
continue;
if(!kmp(s[k],tans))
{
f1 = 1;
break;
}
}
if(f1==0)
{
if(strcmp(ans,tans)>0)
{
strcpy(ans,tans);
flag = 1;
}
}
}
}
if(flag==1)
printf("%s\n",ans);
else
printf ("IDENTITY LOST\n");
}
return 0;
}
本文探讨了在多个商标中寻找最长公共子串作为新的企业标识的方法,通过KMP算法匹配并强调这一共通元素,实现企业形象的统一与传承。
&spm=1001.2101.3001.5002&articleId=100094948&d=1&t=3&u=c13d2b20b89d44ce86b6bb8779baa5aa)
385

被折叠的 条评论
为什么被折叠?



