HDU - 2328 Corporate Identity

    技术2022-07-10  134

    Corporate Identity Corporate Identity Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4935 Accepted Submission(s): 1824

    Problem 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

    Source CTU Open Contest 2007

    Recommend teddy

    大致题意:给你n个字符串,求他们的最长公共字串。 思路:给任意一个字串,从长取到短,依次判断是否在其他字符串中出现过。

    #include <iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; const int maxn = 4110; int ne[maxn]; bool cmp(const string a, const string b) { return a < b; } void GetNext(string p) { /*模式串的长度*/ int pLen = p.size(); ne[0] = -1; int k = -1; int j = 0; while(j < pLen) { if (k == -1 || p[j] == p[k]) { ++j; ++k; ne[j] = k; } else { k = ne[k]; } } } int KmpSearch(string s, string p) { int i = 0; int j = 0; int sLen = s.size(); int pLen = p.size(); while (i < sLen && j < pLen) { if (j == -1 || s[i] == p[j]) { i++; j++; } else { j = ne[j]; } } if (j == pLen) return i - j; else return -1; } string s[maxn]; int main() { int n; while(~scanf("%d", &n)){ if(n == 0)break; for(int i = 0; i < n; i++){/*输入n个字符串*/ cin>>s[i]; } string answer = ""; int len = s[0].size(); int i; for(i = len; i >= 1; i--){ for(int j = 0; j + i <= len; j++){ string subs = s[0].substr(j, i); // cout<<subs<<endl; GetNext(subs);/*对当前模式串取next数组*/ int k; for(k = 1; k < n; k++){ int ans = KmpSearch(s[k], subs); if(ans == -1)break; } if(k == n)/*每个串都匹配*/ { // cout<<1<<endl; if(answer.size() == 0) answer = subs; else answer = min(answer, subs); } } if(answer.size() != 0) break; } if(answer.size() != 0) cout<<answer<<endl; else cout<<"IDENTITY LOST"<<endl; } return 0; } /* 3 aabbaabb abbababb bbbbbabb 2 xyz abc 0 */
    Processed: 0.009, SQL: 9