[kmp][暴力]Corporate Identity HDU2328
作者:互联网
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
题意: 给出n个字符串,求它们的最长公共子串。
分析: 观察到数据范围很小,可以尝试暴力匹配,思路类似[字符串哈希][暴力]Substrings HDU1238_Cloth的博客-CSDN博客这道题,先枚举第一个字符串的所有子串,之后看第2~n个字符串是否存在该子串。
具体代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int _next[205], n, length[4005];
char s[4005][205];
signed main()
{
while(~scanf("%d", &n))
{
if(!n)
break;
for(int i = 1; i <= n; i++)
{
scanf("%s", s[i]+1);
length[i] = strlen(s[i]+1);
}
bool ans = false;
string res;
//开始枚举第一个串的所有子串
for(int len = *min_element(length+1, length+1+n); len >= 1; len--)
{
for(int l = 1; l+len-1 <= length[1]; l++)
{
int r = l+len-1;
//求待匹配字符串的next数组
_next[l] = l-1;
for(int i = l+1, j = l-1; i <= r; i++)
{
while(j != l-1 && s[1][j+1] != s[1][i])
j = _next[j];
if(s[1][j+1] == s[1][i])
j++;
_next[i] = j;
}
for(int k = 2; k <= n; k++)
{
bool flag = false;
for(int i = 1, j = l-1; i <= length[k]; i++)
{
while(j != l-1 && s[1][j+1] != s[k][i])
j = _next[j];
if(s[1][j+1] == s[k][i])
j++;
if(j == r)
{
flag = true;
break;
}
}
if(!flag)
break;
if(k == n)
{
if(!ans)
for(int i = l; i <= r; i++)
res = res + s[1][i];
else
{
string t;
for(int i = l; i <= r; i++)
t = t + s[1][i];
res = min(res, t);
}
ans = true;
}
}
}
if(ans)
break;
}
if(res != "")
printf("%s\n", res.c_str());
else
puts("IDENTITY LOST");
}
return 0;
}
标签:task,HDU2328,will,their,trademarks,new,include,Corporate,Identity 来源: https://blog.csdn.net/m0_55982600/article/details/120515980