H - Corporate Identity(KMP+枚举)

题目

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;
}

发布了52 篇原创文章 · 获赞 2 · 访问量 909

猜你喜欢

转载自blog.csdn.net/qq_44714572/article/details/97265781
今日推荐