admin管理员组

文章数量:1616032

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

思路:

其实这题可以kmp优化一下。
我直接暴力+STL过的。1600ms.还好给了3000ms时间。
但是有个小技巧,枚举长度最短的字符串的子串,以保证时间最少。
注意输出字典序最小的。STL默认排序就是字典序。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

vector<string> vec;
vector<string> ans;
int minindex,minlength;
bool judge(string kk)
{
   int siz = vec.size();

   for(int i=0;i<siz;i++)
   {
       if(vec[i].find(kk)>5000)
       {
           return false;
       }
   }
   return true;
}

int main()
{
    int t;
    string tmp;
    while(cin>>t&&t)
    {
        minindex;
        minlength=1<<30;
       for(int i=0;i<t;i++)
       {
           cin>>tmp;
           vec.push_back(tmp);
           if(tmp.length()<minlength)
           {
               minlength=tmp.length();
               minindex=i;
           }
       }
        bool flag=0;
        tmp=vec[minindex];

        string str;
        for(int i=minlength;i>=1;i--)
        {
            for(int j=0;j<minlength;j++)
            {
               str="";
               for(int k=0;k<i&&j+k<minlength;k++)
               {
                    str+=tmp[j+k];
               }
               if(str.length()==i)
               {
                   if(judge(str))
                   {
                       flag=1;
                       ans.push_back(str);
                   }
               }
            }
            if(flag) break;
        }
        if(ans.size()==0)
        {
            printf("IDENTITY LOST\n");
        }
        else
        {
            sort(ans.begin(),ans.end());
            cout<<ans[0]<<endl;
        }
        vec.clear();
        ans.clear();
    }
    return 0;
}

本文标签: CorporateIDENTITY