hdu2328 Corporate Identity【string库使用】【暴力】【KMP】

Corporate Identity

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3308    Accepted Submission(s): 1228


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
 
Recommend
teddy

题意:

给定n个串,问他们的最长公共子串是什么。

思路:

因为串长是200,串的个数是4000。暴力枚举一个串的所有子串的话是200 * 200.

枚举子串和其他串匹配,统计个数【暴力这么过去了其实是有点虚的。】

尝试用了一下string中的substr和find函数。

substr(j,len)表示从s[j]开始取len长度的子串(包括j)。这题的样例也 太烂了,怎么都过得去。

当然这题应该也能用后缀数组做。

 1 //#include<bits/stdc++>
 2 #include<stdio.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<stdlib.h> 
 7 
 8 #define LL long long
 9 #define ull unsigned long long
10 #define inf 0x3f3f3f3f 
11 
12 using namespace std;
13 
14 int n;
15 const int maxn = 4005;
16 const int maxlen = 205;
17 string s[maxn]; 
18 
19 int main()
20 {
21     while(scanf("%d", &n) && n){
22         for(int i = 0; i < n; i++){
23             cin >> s[i];
24         }
25         int len = s[0].length();
26         int ans = 0;
27         string ansch;
28         for(int i = 1; i <= len; i++){
29             for(int j = 0; j <= len - i; j++){
30                 int tot = 0;
31                 for(int k = 1; k < n; k++){
32                     if(s[k].find(s[0].substr(j, i)) == string::npos){
33                         break;
34                     }
35                     else tot++;
36                 }
37                 if(tot == n - 1){
38                     if(i > ans || i == ans && s[0].substr(j, i) < ansch){
39                         ansch = s[0].substr(j, i);
40                         ans = i;
41                     }
42                 }
43             }
44         }
45         
46         if(ansch != ""){
47             cout<<ansch<<endl; 
48         }
49         else{
50             cout<<"IDENTITY LOST
";
51         }
52     }
53 }
原文地址:https://www.cnblogs.com/wyboooo/p/10259407.html