POJ-3450-Corporate Identity(KMP)

题意:给你n个的串,求出它们的最长公共子串,如果不存在这个子串,则输出“IDENTITY LOST”,如果存在多个最长公共子串,则输出字典序最小的那一个。

思路:二分枚举最长公共子串的长度,然后再枚举此长度的所有子串,找出字典序最小的解(不用二分直接暴力也能过)

//400 KB	125 ms
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[4010][210];
char minstr[210],tmp[210];
int next[210];
int lens[4010];
int n;
void getnext()
{
    next[0]=-1;
    int i=0,j=-1;
    while(tmp[i]!=0){
        while(j>-1&&tmp[i]!=tmp[j])
            j=next[j];
        j++;
        i++;
        next[i]=j;
    }
}
bool KMP()
{
    getnext();
    int lenm=strlen(tmp);
    for(int k=1;k<=n;k++){
        int i=0,j=0;
        while(i<lens[k]&&j<lenm){
            if(j==-1||str[k][i]==tmp[j]){
                i++;
                j++;
            }
            else j=next[j];
        }
        if(j!=lenm) return false; //只要有一个匹配失败则失败
    }
    return true;
}
bool ok(int x)
{
    int len=strlen(minstr);
    for(int i=0;i<=len-x;i++){
        strncpy(tmp,minstr+i,x); //从minstr+i的位置复制x长度到tmp中
        tmp[x]='\0';            //末尾加上'\0'把他配成完整一个字符串,重要细节

        if(KMP()) return true;
    }
    return false;
}
int main()
{
    while(scanf("%d",&n),n){
        int minn=(1<<30);
        for(int i=1;i<=n;i++) {
            scanf("%s",str[i]);
            lens[i]=strlen(str[i]);
            if(minn>lens[i]){
                minn=lens[i];
                strcpy(minstr,str[i]);
            }

        }
        int lb=0,ub=minn+1;  //左边界和右边界开在范围之外,有益处
        while(ub-lb>1){  //二分出匹配的最大长度
            int mid=(lb+ub)/2;
            if(ok(mid)) lb=mid;
            else ub=mid;
        }

        char ans[210],first=1;
        for(int i=0;lb&&i<=minn-lb;i++){ //以这个最大长度再确定字典序最小的匹配串
            strncpy(tmp,minstr+i,lb);
            tmp[lb]='\0';
            if(KMP()){
                if(first) {
                    first=0;
                    strcpy(ans,tmp);
                }
                else {
                    if(strcmp(ans,tmp)>0)
                        strcpy(ans,tmp);
                }
            }
        }
        if(lb) printf("%s\n",ans);
        else printf("IDENTITY LOST\n");
    }
    return 0;
}
时间: 2024-10-15 15:55:37

POJ-3450-Corporate Identity(KMP)的相关文章

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

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