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

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 }

原文地址:https://www.cnblogs.com/wyboooo/p/10259407.html

时间: 2024-07-31 08:10:47

hdu2328 Corporate Identity【string库使用】【暴力】【KMP】的相关文章

hdu2328 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

[HDU2328]Corporate Identity(后缀数组)

传送门 求 n 个串的字典序最小的最长公共子串. 和 2 个串的处理方法差不多. 把 n 个串拼接在一起,中间连上一个没有出现过的字符防止匹配过界. 求出 height 数组后二分公共子串长度给后缀数组分组. 然后 check,每一组中是否所有的字符串都包含. 直接遍历 sa 数组,第一个满足的结果就是字典序最小的. ——代码 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define

POJ 题目3450 Corporate Identity(KMP 暴力)

Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5493   Accepted: 2015 Description Beside other services, ACM helps companies to clearly state their "corporate identity", which includes company logo but also other

POJ 3450 Corporate Identity KMP题解

本题要求求一组字符串的最长公共子串,其实是灵活运用KMP快速求最长前缀. 注意肯爹的题意:要求按照字典顺序输出. 还有要提醒的就是:有人也是用KMP来解这道题,但是很多人都把KMP当成暴力法来用了,没有真正处理好细节,发挥KMP的作用.而通常这些人都大喊什么暴力法可以解决本题,没错,的确暴力法是可以解决本题的,本题的数据不大,但是请不要把KMP挂上去,然后写成暴力法了,那样会误导多少后来人啊. 建议可以主要参考我的getLongestPre这个函数,看看是如何计算最长前缀的. 怎么判断你是否把本

(KMP 暴力)Corporate Identity -- hdu -- 2328

http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 698    Accepted Submission(s): 281 Problem Description Beside other services, AC

POJ-3450 Corporate Identity (KMP+后缀数组)

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 a

hdu 2328 Corporate Identity(kmp)

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 recentl

HDU - 2328 Corporate Identity

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 a

N - Corporate Identity

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