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

题目大意:输入n,代表有n个串,问你m个串中最长公共子串是什么,如果有长度相同的,输出字典序最小的

思路:也算是KMP的裸题了,但是注意一下这题要有一个优化操作,找出长度最短的串,以这个串为基础寻找是否有子串,还有值得一提的是:那个C++加速的东西好像没有一点用,一直超时,改成c的

输入输出才过了,就因为这个卡了好几天,难受。。。

看代码1:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1000;
const int maxn=4e3+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn][210];
int next[maxn];
void cal_next(char s[])
{
    int len=strlen(s);
    int k=-1;
    next[0]=-1;
    for(int i=1;i<len;i++)
    {
        while(k>-1&&s[k+1]!=s[i])
        {
            k=next[k];
        }
        if(s[k+1]==s[i]) k++;
        next[i]=k;
    }
}
bool kmp(char x[],char y[])
{
    int k=-1;
    int len1=strlen(x);
    int len2=strlen(y);
    for(int i=0;i<len1;i++)
    {
        while(k>-1&&y[k+1]!=x[i])
        {
            k=next[k];
        }
        if(x[i]==y[k+1]) k++;
        if(k==len2-1) return true;
    }
    return false;
}
int main()
{
    //ios::sync_with_stdio(false);
    int n,flag=0;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        if(n==0) break;
        char ans[210]="",temp[210];
        scanf("%s",a[0]);
        getchar();
        strcpy(temp,a[0]);//这里不用自己加‘\0‘,因为a[0]本身就有‘\0‘的,直接复制过去了
        for(int i=1;i<n;i++)
        {
            scanf("%s",a[i]);
            getchar();
            if(strlen(a[i])<strlen(temp))
            {
                strcpy(temp,a[i]);
            }
        }
        int len=strlen(temp);
        for(int i=0;i<len;i++)//起点
        {
            for(int j=1;i+j<=len;j++)//长度
            {
                char op[210];
                strncpy(op,temp+i,j);//不会在结尾给你自动加‘\0‘
                op[j]=‘\0‘;//注意这里手动加一个‘\0‘,因为你只是得到了那几个字符,并没有得到‘\0‘
                cal_next(op);
                for(int k=0;k<n;k++)
                {
                    flag=0;
                    if(!kmp(a[k],op))
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==1)//有一个找不到就不用遍历以该起点长度更大的子串了,因为肯定也会找不到
                    break;
                else
                {
                    if(strlen(op)>strlen(ans)) strcpy(ans,op);
                    else if(strlen(op)==strlen(ans)&&strcmp(ans,op)>0)
                    {
                        strcpy(ans,op);
                    }
                }
            }
        }
        if(strlen(ans)==0)
            printf("IDENTITY LOST\n");
        else
            printf("%s\n",ans);
    }
    return 0;
}

看代码2:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1000;
const int maxn=5e3+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn][210];
int next[maxn];
void cal_next(char op[])
{
    int k=-1;
    next[0]=-1;
    int len=strlen(op);
    for(int i=1;i<len;i++)
    {
        while(k>-1&&op[k+1]!=op[i])
        {
            k=next[k];
        }
        if(op[k+1]==op[i]) k++;
        next[i]=k;
    }
    return ;
}
bool kmp(char x[],char y[])
{
    int k=-1;
    int len1=strlen(x);
    int len2=strlen(y);
    for(int i=0;i<len2;i++)
    {
        while(k>-1&&x[k+1]!=y[i])
        {
            k=next[k];
        }
        if(x[k+1]==y[i]) k++;
        if(k==len1-1) return true;
    }
    return false;
}
int main()
{
    int n,flag;
    char b[210],temp[210];
    while(scanf("%d",&n)!=EOF)
    {
        int minn=10000;
        if(n==0) break;
        getchar();
        char ans[210]="";
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            int len=strlen(a[i]);
            if(len<minn)
            {
                minn=len;
                strcpy(b,a[i]);
            }
            getchar();
        }
        for(int i=1;i<=minn;i++)//长度
        {
            for(int j=0;j+i<=minn;j++)//起点
            {
                flag=0;
                strncpy(temp,b+j,i);
                temp[i]=‘\0‘;

                cal_next(temp);
                for(int k=0;k<n;k++)
                {
                    if(!kmp(temp,a[k]))
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==0)
                {
                    if(strlen(temp)>strlen(ans)) strcpy(ans,temp);
                    else if(strlen(temp)==strlen(ans)&&strcmp(ans,temp)>0) strcpy(ans,temp);
                }
            }
        }
        if(strlen(ans)==0) printf("IDENTITY LOST\n");
        else
        {
            printf("%s\n",ans);
        }
    }
}

原文地址:https://www.cnblogs.com/caijiaming/p/9718603.html

时间: 2024-10-10 16:47:58

POJ - 3450的相关文章

POJ 3450 Corporate Identity KMP题解

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

字符串截取模板 &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 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

POJ3080 &amp;&amp; POJ 3450

题意: 给定m个串,让你找出它们的最长公共子串 思路: 先二分串的长度,枚举该长度的串(可以从第一个串里找),看该长度是否合法,(就是用这个长度的所有串去匹配给定的 第 2 - m 个串 为提高效率,用kmp)然后得到一个最大长度,再在该长度下寻找一个字典序最小的解即可 POJ3450代码(3080类似) /************************************************************************* > File Name: poj3450.

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

kuangbin带你飞 后缀数组 题解

2份模板 DC3 . 空间复杂度O3N 时间复杂度On #define F(x) ((x) / 3 + ((x) % 3 == 1 ? 0 : tb)) #define G(x) ((x) < tb ? (x) * 3 + 1 :((x) - tb) * 3 + 2) const int MAXN = 300010; const int MAXM = 100010; char input[MAXM]; int wa[MAXN],wb[MAXN],ws[MAXN],wv[MAXN],wsd[MAX

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

hdu 1541/poj 2352:Stars(树状数组,经典题)

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4052    Accepted Submission(s): 1592 Problem Description Astronomers often examine star maps where stars are represented by points on a plan

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&