hdu2328 Corporate Identity【string庫使用】【暴力】【KMP】
阿新 • • 發佈:2019-01-12
spa only eas () 暴力枚舉 custom c++ several letter
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
Corporate Identity
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3308 Accepted Submission(s): 1228
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.
After the last trademark, the next task begins. The last task is followed by a line containing zero.
Sample Output abb IDENTITY LOST
Source CTU Open Contest 2007
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\n"; 51 } 52 } 53 }
hdu2328 Corporate Identity【string庫使用】【暴力】【KMP】