POJ3450 Corporate Identity 【KMP 暴力strstr可过】

题目描述

给你n个字符串,问其最长的公共串是啥。如果长度都是最长,输出字典序小的。

Sample Input

3

aabbaabb

abbababb

bbbbbabb

2

xyz

abc

0

Sample Output

abb

IDENTITY LOST

解题思路

取第一个字符串O(n^2)遍历,对其他字符串直接strstr匹配即可。 用KMP当然最好。

用ans存放当前的最优解。

下面是代码

#include <cstdio>
#include <cstring>
const int maxn = 210;
int n;
int anslen = 0;
char ans[maxn];
char s[4010][maxn];
char p[maxn];//模式串
int main()
{
    while(scanf("%d",&n) && n) {
        anslen = 0;
        for(int i = 0 ; i < n ; i ++) scanf("%s",s[i]);
        /** 遍历s[0] */
        int len1 = strlen(s[0]);
        for(int i = 0 ; i < len1 ; i ++) {
            for(int j = i ; j < len1 ; j ++) {
                /** 模式串是s[0][i,i+1,...,j] */
                for(int k = i ; k <= j ; k ++) {
                    p[k-i] = s[0][k];
                }
                p[j-i+1] = '\0';
                /** 模式串已就绪 */
                bool flag = true;//记录其他主串是否都有p串
                int len = j-i+1;//长度
                if(len < anslen) continue;
                for(int k = 1 ; k < n ; k ++) {
                    if(strstr(s[k],p) == NULL) {
                        flag = false;
                        break;
                    }
                }
                if(flag) {
                    if(len == anslen) {//看字典序
                        if(strcmp(p,ans) < 0) {
                            strcpy(ans,p);
                        }
                    }else {
                        strcpy(ans,p);
                        anslen = len;
                    }
                }
            }
        }
        if(anslen > 0) printf("%s\n",ans);
        else printf("IDENTITY LOST\n");
    }
    return 0;
}
时间: 2024-08-13 23:46:41

POJ3450 Corporate Identity 【KMP 暴力strstr可过】的相关文章

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

POJ 3450 Corporate Identity KMP题解

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

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

POJ-3450 Corporate Identity

枚举一个串的所有子串,和其余的串匹配,裸kmp的题. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 222; const int maxd = 4010; int f[maxn]; char str[maxn]; char t_str[maxn]; char c_str[maxd][maxn]; void init_kmp(char

POJ3450 Corporate Identity

后缀数组. 解决多个字符串的最长公共子串. 采用对长度的二分,将子串按height分组,每次判断是否在每个字符串中都出现过. 复杂度O(NlogN) By:大奕哥 1 #include<cstring> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<iostream> 6 #define rank fank 7 using namespace std

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

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 "cor

(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

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