poj3450

news/2025/3/12 12:57:36/

Corporate Identity

http://poj.org/problem?id=3450

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

题意

求多个字符串的最大公共子串。

题解

求多个字符串的最大公共子串,我们将第一个字符串和去其他字符串匹配出最大公共子串,匹配的方法是将第一个字符串的每一位都作为子串起点来查找子串的终点,通过kmp可以将时间压缩到94ms, 有个大佬的79ms。代码如下。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<cstdlib>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int next[205];
char str[4005][205];
char res[205];
int n;void GetNext(char *s,int len)//普通的kmp
{memset(next,0,sizeof(next));next[0]=-1;int i=0,j=-1;while(i<len){if (j==-1||s[i]==s[j]) next[++i]=++j;else j=next[j];}
}int GetLongestStr(char *s,int len)
{GetNext(s,len);for(int i=1;i<n;i++)//与每一个字符串比较,求最大公共子串{char *p=str[i];int j=0,tmp=0;while(*p&&j<len)//j<len保证是公共的最大字串{if(j==-1||*p==s[j]) {p++;j++;tmp=max(tmp,j);}else {j=next[j];}}len=tmp;}return len;
}
/*
为什么GetLongestStr这样就是最大的公共字串呢?
前面说了字串起点固定求终点,也就是说这里str与s比较每一次都从s[0]开始,s[0]就是固定的起点,然后查找终点,配合j<len就可以得到公共的最大的字串。
这样只需要枚举第一个字串长度就可以了。
*/
int main(void)
{while(~scanf("%d",&n)&&n){getchar();for(int i=0;i<n;i++)scanf("%s",str[i]);int ans=0,len=strlen(str[0]),pos=0;for(int i=0;i<len;i++)//求以第i位为起点的最大公共子串 {if(ans>len-i) break;//理论上是可以减少用时的,但实际上并没有 int tmp=GetLongestStr(str[0]+i,len-i);if(tmp>=ans){if(tmp>ans){ans=tmp;pos=i;}else {bool smaller=true;for(int j=0;j<ans;j++){if(str[0][pos+j]>str[0][i+j]) break; //这里为什么能这样比较呢?else if(str[0][pos+j]<str[0][i+j]) //因为起点是固定从i开始的,{ //所以这里就可以用上一次的起点pos和长度ans与这一次的起点i和tmp比较smaller=false;break;}}if(smaller) pos=i;}}}if(ans)for(int i=0;i<ans;i++)printf("%c",str[0][i+pos]);elseprintf("IDENTITY LOST");printf("\n");}return 0;
}

萌新一个,如有错误,欢迎指正。


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

相关文章

HDU 3450(树状数组)

题目链接:HDU 3450 题目大意: 求长度大于等于且相邻差值不超过d的子序列的个数. 分析: 没什么好说的水题,就是没给a的范围真的天坑. 以下是代码: #include <set> #include <map> #include <stack> #include <queue> #include <vector> #inclu…

HDU 3450 Counting Sequences(树状数组+离散化+二分+dp思想 详细解答)

题目链接&#xff1a;HDU 3450 题意&#xff1a;给出一段序列&#xff0c;让你找出一段子序列&#xff08;长度>2&#xff09;满足序列中每相邻的两个数之间的差 <d&#xff0c;求出这样的序列的个数。 思路&#xff1a;本题利用dp思想&#xff0c;dp[i] 表示 以 i 为结…

小米路由器R1C或R1CM小米R1C 原厂Bootloader和epproom

想把刷机后的小米路由器R1C刷回小米原厂,而又没备份的小伙伴可以使用下面的BootLoader和epprom&#xff08;也就是原厂分区里面的Factory分区&#xff09;都是我自己手动备份的。 分享给大家 链接&#xff1a;https://pan.baidu.com/s/1v6iUB5idgxUPqclk2KJLkA 提取码&#x…

android 7.1 小米,你的小米手机,能升Android 7.1吗?

对于诸多资深Android手机用户来说&#xff0c;官方何时放出最新Android系统更新包&#xff0c;是他们所关心的&#xff0c;小米手机用户也是如此。今天MIUI官方汇总了支持Android N的小米机型。你的手机能升Android 7.1吗&#xff1f; 根据MIUI官方的汇总数据(注&#xff1a;7月…

小米全系列机型代码查询与 制作rom分区架构图示

安卓机型的不断更新与芯片型号的不同和各自厂家研发的方向差异导致目前各品牌机型固件架构也不同。这种现象对于普通大众来说没有丝毫影响。一般只有玩家类和第三方rom制作有所关注 来几张图看下当前小米系列机型的代码和分区架构图示 做第三方rom时注意各机型的分区架构和线刷…

小米刷机教程

刷机教程 一、解锁小米手机教程 1、手机进入“设置 -> 开发者选项 -> 设备解锁状态”中绑定账号和设备 2、电脑打开申请解锁小米手机&#xff08;下载解锁工具&#xff09; 3、手机进入Bootloader模式&#xff08;重启&#xff0c;按住音量下键&#xff09; 4、通过…

小米pro15拆机_实战小米笔记本PRO 15.6寸拆解 加装M.2海力士固态硬盘

你是AMD Yes党?还是intel和NVIDIA的忠实簇拥呢?最新一届#装机大师赛#开始啦!本次装机阵营赛分为3A红组、intel NVIDIA蓝绿组、混搭组还有ITX组,实体or虚拟装机都能参与,可使用值得买定制化DIY装机工具在文中展现配置单!每个小组均有精美礼品,优秀文章还可角逐装机大师终…

qq空间把android改成iphone,qq空间改iPhone6 Plus方法 qq空间改手机型号教程

苹果今年发布了两部iPhone&#xff0c;分别是iPhone6和iPhone6 Plus&#xff0c;对于土豪们来说&#xff0c;iPhone6 Plus当然是换机的优先选择&#xff0c;相信随着身边使用iPhone6 Plus的朋友越来越多&#xff0c;势必将在QQ空间掀起一股“您的手机来自 iPhone6 Plus”标识热…