【题解】HDU-2328 Corporate Identity

Corporate Identity(HDU-2328)

题面

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.

输入

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.

输出

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.

样例输入

1
2
3
4
5
6
7
8
3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0

样例输出

1
2
abb
IDENTITY LOST

提示

思路

【题解】POJ-3080 Blue Jeans,枚举子串,暴力KMP即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
char s[mxn][mxm], t[mxm], ans[mxm];
int nxt[mxm];

void getnxt(char* t, int m)
{
int i = 0, j = -1; nxt[0] = -1;
while (i < m)
{
if (j == -1 || t[i] == t[j]) {
i++, j++;
// if (t[i] == t[j])
// nxt[i] = nxt[j]; // next数组优化
// else
nxt[i] = j;
} else
j = nxt[j];
}
}

int KMP(char* s, char* t, int n, int m)
{
int i = 0, j = 0, ans = 0;
while (i < n)
{
if (j == -1 || s[i] == t[j]) {
i++, j++;
if (j >= m) { // 匹配
// ans++;
// j = nxt[j];
return i-j;
}
} else
j = nxt[j];
}
// return ans;
return -1;
}

int main()
{
int n;
while(~scanf("%d", &n) && n)
{
for(int i=0; i<n; i++) scanf("%s", s[i]);
int sl = strlen(s[0]), f=0;

for(int i=0; i<sl; i++) // 枚举模式串起点
{
int tl = 0, k;
for(int j=i; j<sl; j++) // 枚举模式串长度
{
t[tl++] = s[0][j];
t[tl] = '\0';
getnxt(t, tl);
for(k=0; k<n; k++) // 枚举所有文本串
if(KMP(s[k], t, sl, tl) == -1)
break;
if(k>=n) // 所有串公共子串
{
if(!f || tl>strlen(ans)){ // 长度最长
strcpy(ans, t); f=1;
}else if(tl==strlen(ans) && strcmp(t, ans)<0){ // 字典序最小
strcpy(ans, t);
}
}
}
}
if (!f)
printf("IDENTITY LOST\n");
else
printf("%s\n", ans);
}
return 0;
}