POJ - 3450

news/2025/2/5 14:46:07/

题目链接: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");elseprintf("%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


http://www.ppmy.cn/news/201296.html

相关文章

BZOJ 3450 Easy

Description 某一天WJMZBMR在打osu~~~但是他太弱逼了&#xff0c;有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有n次点击要做&#xff0c;成功了就是o&#xff0c;失败了就是x&#xff0c;分数是按comb计算的&#xff0c;连续a个comb就有a*a分&#xff0c;comb就是极大…

hdu3450

开始看觉得是dp&#xff0c;复杂度O(n^2)会超时就没做&#xff0c;应该是用线段树或者树状数组&#xff0c;加上离散化和二分法优化。 你需要一个dp[i]数组&#xff0c;存储的是以i为结尾的个数&#xff0c;结果就是dp[]之和. 离散化的部分是用一个离散化结构体&#xff0c;最…

3450

/* KMP来做532ms */// include file #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cctype> #include <ctime>#include <iostream> #include <sstream> #include <fstream> #in…

BZOJ3450 Easy

原题链接&#xff1a;http://www.lydsy.com/JudgeOnline/problem.php?id3450 Easy Description 某一天WJMZBMR在打osu~~~但是他太弱逼了&#xff0c;有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有n次点击要做&#xff0c;成功了就是o&#xff0c;失败了就是x&…

tjut 3450

/*分析:每次寻找以a[i]结尾的子序列能有多少个,只需要寻找a[i]-d和a[i]d之之间数结尾的子序列 的全部个数,然后把a[i]直接放在那些数后面即可 找寻和更新都是用树状数组 */ #include <iostream> #include <cstdio> #include <cstdlib> #include …

POJ 3450题解

题目连接 POJ 3450 Corporate Identity 思路 先用后缀数组处理出height数组&#xff0c;然后我们二分最长公共子串的长度&#xff0c;然后再height中找大于这个长度并且不在一个字符串的个数&#xff0c;如果等于n则返回true否则返回false即可。 前期一直TLE不知道为何&…

hdu 3450

吐吐槽吧&#xff1a;本来思路完全对了&#xff0c;但是那个二分查找搞错了&#xff0c;我还以为一个就可以了&#xff0c;结果查找上界和下界分别要一个查找函数&#xff08;WA时就看了一下别人的代码&#xff0c;发现别人都是用两个查找函数的&#xff0c;模仿别人写的查找函…

poj3450

Corporate Identity http://poj.org/problem?id3450 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 Bu…