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 nxt[M],n;
char dict[N][M];
void get_nxt(char *a,int n){
    int i,j;
    for(nxt[0]=j=-1,i=1;i<n;nxt[i++]=j){
        while(~j&&a[j+1]!=a[i])j=nxt[j];
        if(a[j+1]==a[i])j++;
    }
}
int LongestPre(char *s,int len){
    get_nxt(s,len);
    for(int i=1;i<n;i++){
        char *p=dict[i];
        int ans=0;
        for(int j=-1;*p;p++){
            while(~j&&s[j+1]!=*p)j=nxt[j];
            if(s[j+1]==*p){
                j++; ans=max(ans,j+1);
            }if(j==len-1)j=nxt[j];
        }len=min(len,ans);
    }return len;
}
int main(){
    while(scanf("%d",&n)&&n){
        getchar();
        for(int i=0;i<n;i++)gets(dict[i]);
        int len=strlen(dict[0]),ans=0,pos=0;
        for(int i=0;i<len;i++){
            int tmp=LongestPre(dict[0]+i,len-i);
            if(tmp>=ans){
                if(tmp>ans)ans=tmp,pos=i;
                else{
                    bool flag=1;
                    for(int t=0;t<ans;t++){
                        if(dict[0][pos+t]>dict[0][i+t])break;
                        else if(dict[0][pos+t]<dict[0][i+t]){flag=0;break;}
                    }if(flag)pos=i;
                }
            }
        }if(ans){
            for(int i=0;i<ans;i++)putchar(dict[0][pos+i]);
                puts("");
        }else puts("IDENTITY LOST");
    }return 0;
}

  

时间: 2024-10-13 02:20:16

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

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这个函数,看看是如何计算最长前缀的. 怎么判断你是否把本

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 2406 Power Strings(kmp)

Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33335   Accepted: 13852 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def"

poj 3080 Blue Jeans(kmp)

Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you have been tas

POJ 题目3461 Oulipo(KMP)

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26479   Accepted: 10550 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quot

poj 3080 Blue Jeans (KMP)

http://poj.org/problem?id=3080 Blue Jeans Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populate

POJ 2185 Milking Grid(KMP)

解题思路: 算是一个多维的KMP,其实是一样的,不过把1个字符的比较改成一行或一列字符的比较,对行和列使用两次KMP,最后乘起来就可以了. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include &l

POJ - 2406 - Power Strings (KMP)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 34601   Accepted: 14319 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "