H - Corporate Identity(KMP+枚举)

ACM帮助企业清晰表达“企业标识”,包括标志和其他符号,如商标。IBM希望保留现有标志,只改变商标以展现新身份。任务是在所有商标中找到最长的共同字母序列,以形成新的强调图形标识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

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

题意

	求多个字符串共有的最长字串

解释

通过substr函数取出第一个字符串中的子串,对每个子串进行操作, 与第二个到第n个字符串进行kmp搜索,若找不到跳出,则k<n。若k == n,对该子串与已记录的最长子串进行比较谁长选谁,一样长选字典序列小的子串(通过compare函数比较字典序列), 对于初始的最长子串通过flag来使其成为第一个满足是所有串的子串,这一条件的子串。最后也通过flag来判断是否存在一个满足是所有串的子串,这一条件的子串。

#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>
#define mod 10007
using namespace std;

string str[4005];
int Next[4005];
void getnext(string b, int lenb){
    Next[0] = -1;
    int i = 0, j = -1;
    while(i < lenb){
        if(j == -1 || b[i] == b[j]){
            i++;
            j++;
            Next[i] = j;
        }
        else
            j = Next[j];
    }

}
int kmp(string b, int lenb, string a, int lena){
    getnext(b, lenb);
    int i = 0; int j = 0;
    while(i < lena){
        if(j == -1 || b[j] == a[i]){
            i++;
            j++;
        }
        else
            j = Next[j];
        if(j == lenb)
            return 1;
    }
    return 0;

}
int main(){
    int  n, i, j, k;
    while(~scanf("%d", &n) && n){
        for(i = 0; i < n; i++)
            cin >> str[i];
        int flag = 0;
        string ans;
        for(i = 0; i < str[0].size(); i++){
            for(j = i; j < str[0].size(); j++){
                string b = str[0].substr(i, j-i+1);
                for(k = 1; k < n; k++)
                    if(!kmp( b, b.size(), str[k], str[k].size()))
                        break;

                if(k == n){
                    if(!flag || b.size() > ans.size()){
                        ans = b;
                        flag = 1;
                    }
                    else if(b.size() == ans.size() && b.compare(ans) < 0)
                            ans = b;
                }
            }
        }
        if(!flag)
            printf("IDENTITY LOST\n");
        else
            cout << ans <<endl;
    }


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值