Corporate Identity
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8538 | Accepted: 2853 |
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
题意:求多个串的最长公共子串
网上的思路大多没看懂,所以自己琢磨出来个方法
首先对第一个串建立后缀自动机,然后让剩下的串依次跑一变,每到一个状态说明匹配到了一个子串,那么现在有两个问题:
1 如何判断某个状态是否已经被剩下的所有串遍历到?
2 怎么知道那个状态所代表的的最长子串长度
对于第一个问题,开一个数组记录一下匹配到的次数即可
而第二个问题,用到了两个数组L[]和LL[],L[i]表示到当前串为止第i位状态所能匹配的最小长度,LL[i]表示当前正在匹配的字符串所匹配到的第i个状态所能匹配的最大长度那么L[i]=min(L[i],LL[i]),这样也解决了。
注意一个问题,如果匹配到了某一状态,不仅要更新当前状态i信息,也必须要更新所有link[i]的信息。
比如:
abb
ab
b
如果不更新link的信息,那么答案为空串,因为在第二个串时,并不会跑到root->b的状态,而其实这也是一个匹配。
剩下的就是输出答案了,只要在建立后缀自动机的时候多记一个状态位置。
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxm = 1005;
struct node
{
int link, len, Next[26], pos;
}st[maxm];
struct stu
{
char ch[205];
bool operator<(const stu &r)const
{
return strcmp(ch, r.ch) < 0;
}
}p[maxm];
int cnt, last, s[maxm], L[maxm], LL[maxm], ss[maxm], pre[maxm];
char str[maxm], ch[maxm], sh[maxm], b[maxm];
void init()
{
cnt = 0;
memset(st, 0, sizeof(st));
memset(s, 0, sizeof(s));
st[0].link = -1, last = 0;
cnt++;
}
void insert(int c, int id)
{
int p = last, now = cnt++, rev;
st[now].len = st[p].len + 1;
st[now].pos = id;
L[now] = st[now].len;
while (p != -1 && !st[p].Next[c])
st[p].Next[c] = now, p = st[p].link;
if (p == -1) st[now].link = 0;
else
{
int q = st[p].Next[c];
if (st[p].len + 1 == st[q].len)
st[now].link = q;
else
{
rev = cnt++;
st[rev] = st[q];
st[rev].pos = id;
L[rev] = st[rev].len = st[p].len + 1;
while (p != -1 && st[p].Next[c] == q)
st[p].Next[c] = rev, p = st[p].link;
st[q].link = st[now].link = rev;
}
}
last = now;
}
void work()
{
int p = 0, now = 0, i, c, q;
for (i = 0;str[i] != '\0';i++)
{
c = str[i] - 'a';
if (st[p].Next[c])
{
now++, p = st[p].Next[c];
ss[p]++, LL[p] = max(LL[p], now);
q = st[p].link;
while (q != -1)
{
LL[q] = max(LL[q], st[q].len);
ss[q]++, q = st[q].link;
}
}
else
{
while (p != -1 && !st[p].Next[c])
p = st[p].link;
if (p == -1) p = 0, now = 0;
else
{
now = st[p].len + 1, p = st[p].Next[c];
ss[p]++, LL[p] = max(LL[p], now);
q = st[p].link;
while (q != -1)
{
LL[q] = max(LL[q], st[q].len);
ss[q]++, q = st[q].link;
}
}
}
}
for (i = 1;i < cnt;i++)
{
L[i] = min(L[i], LL[i]);
if (ss[i]) s[i]++;
ss[i] = LL[i] = 0;
}
}
int main()
{
int n, i, j, k, sum, ans, id, tot, t;
while (scanf("%d", &n), n != 0)
{
init();
scanf("%s", b);
for (i = 0;b[i] != '\0';i++)
insert(b[i] - 'a', i);
for (i = 1;i < n;i++)
{
scanf("%s", str);
work();
}
ans = 0;
for (i = 1;i < cnt;i++)
{
if (s[i] == n - 1)
ans = max(ans, L[i]);
}
if (ans == 0)
{
printf("IDENTITY LOST\n");
continue;
}
sum = 0;
for (i = 1;i < cnt;i++)
{
if (s[i] == n - 1 && L[i] == ans)
{
tot = 0, sum++;
for (j = st[i].pos - ans + 1;j <= st[i].pos;j++)
p[sum].ch[tot++] = b[j];
p[sum].ch[tot] = '\0';
}
}
sort(p + 1, p + 1 + sum);
puts(p[1].ch);
}
return 0;
}