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 *s){
    int L = strlen(s);
    f[0] = 0; f[1] = 0;
    for(int i = 1; i < L; i++){
        int j = f[i];
        while(j && s[i] != s[j]) j = f[j];
        f[i + 1] = s[i] == s[j] ? j + 1 : 0;
    }
}
int solve_kmp(char *s,int m){
    int L = strlen(s);
    int j = 0;
    for(int i = 0; i < L; i++){
        while(j && s[i] != t_str[j]) j = f[j];
        if(s[i] == t_str[j]) j++;
        if(j == m) return 1;
    }
    return 0;
}
int main(){
    int n;
    while(scanf("%d",&n) && n){
        scanf("%s",str);              //输入的第一个串,让它的子串作为模板进行匹配
        for(int i = 0; i < n - 1; i++) scanf("%s",c_str[i]);
        int L = strlen(str);
        int len_max = 0;                        //最终长度
        char ret_str[maxn];                     //最终字符串
        for(int i = L; i >= 1; i--){            //枚举子串的时候按照长度从小到大进行枚举
            for(int j = 0; j + i <= L; j ++){
                strncpy(t_str,str + j,i);       //枚举一个子串
                t_str[i + j] = '\0';
                //printf("%d,%d:%s\n",i,strlen(t_str),t_str);
                init_kmp(t_str);                //构造状态转移函数
                int ok = 1;
                for(int k = 0; k < n - 1; k ++) //与每个字符串进行匹配
                    if(!solve_kmp(c_str[k],i)){ //如果没办法匹配
                        ok = 0;
                        break;
                    }
                //if(ok) puts(t_str);
                if(ok && i > len_max){
                     len_max = i;
                     strcpy(ret_str,t_str);
                     //puts(t_str);
                }
                if(ok && i == len_max && strcmp(ret_str,t_str) > 0){
                    //puts(t_str);
                    strcpy(ret_str,t_str);
                }
            }
            if(i < len_max + 1) break;
        }
        if(!len_max) printf("IDENTITY LOST\n");
        else printf("%s\n",ret_str);
    }
    return 0;
}
时间: 2024-08-06 11:57:55

POJ-3450 Corporate Identity的相关文章

POJ 3450 Corporate Identity KMP题解

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

POJ 3450 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

POJ 3450 Corporate Identity(KMP)

[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所有串做kmp,取匹配最小值的最大值就是答案. [代码] #include <cstring> #include <cstdio> #include <algorithm> const int N=4050,M=210; using namespace std; int nx

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 3518 Corporate Identity 后缀数组-&gt;多字符串最长相同连续子串

题目链接 题意:输入N(2 <= N <= 4000)个长度不超过200的字符串,输出字典序最小的最长公共连续子串; 思路:将所有的字符串中间加上分隔符,注:分隔符只需要和输入的字符不同,且各自不同即可,没有必要是最小的字符; 连接后缀数组求解出height之后二分长度,由于height是根据sa数组建立的,所以前面符合的就是字典序最小的,直接找到就停止即可; ps: 把之前的模板简化了下,A题才是关键; #include<iostream> #include<cstdio&

字符串截取模板 &amp;&amp; POJ 3450、3080 ( 暴力枚举子串 &amp;&amp; KMP匹配 )

//截取字符串 ch 的 st~en 这一段子串返回子串的首地址 //注意用完需要根据需要最后free()掉 char* substring(char* ch,int st,int en) { int length = en - st + 1; char* pch=ch; char* subch=(char*)malloc(length+1); pch=pch+st; for(int i=0;i<length;i++) subch[i]=*(pch++); subch[length]='\0';

POJ - 3450

题目链接:http://poj.org/problem?id=3450 Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8549   Accepted: 2856 Description Beside other services, ACM helps companies to clearly state their "corporate identity", which i

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

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

(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