M - Corporate Identity (多个字符串查找相同的子串)

本文介绍了一种用于找出多个商标中最长公共子串的方法,目的是为了帮助公司在更新品牌形象的同时保留原有标识的核心元素。文中提供了两种不同的实现方案,并对比了它们的运行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:给n个字符串,找到这n个字符串中共同的子串,如果有输出长度最大的,如果长度相同输出字典序最小的,如果找不到输出IDENTITY LOST

下面会给出两个代码,第一种的方法是找到一个字符串,然后用找到的第一个字符串再和下面的字符串进行比较

第二种不知道咋来的,是一个大神的代码,第一种的运行时间大约300ms而第二种的速度是43s(可怕)

第一种

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[4005][205],sd[205],ans[205],as[205];
int ne[205];
void getnext(int len)
{
    int i=0,j=-1;
    ne[0]=-1;
    while(i<len)
    {
        if(j==-1||sd[i]==sd[j])
            ne[++i]=++j;
        else
            j=ne[j];
    }
}
int kmp(int x)
{
    int len=strlen(s[x]),i=0,j=0,sum=0;
    while(i<len)
    {
        if(j==-1||sd[j]==s[x][i])
        {
            i++,j++;
            sum=max(j,sum);
        }
        else
            j=ne[j];
    }
    return sum;
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        int b=0;
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        int len1=strlen(s[0]);
        for(int i=0;i<len1;i++)
        {
            int maxx=0x3f3f3f3f;
            strncpy(sd,s[0]+i,len1-i);//从字符串s的第i位后复制len1-i个字符到sd中
            sd[len1-i]='\0';
            getnext(strlen(sd));
            for(int j=1;j<n;j++)
            {
                int a=kmp(j);
                if(maxx>a)
                {
                    maxx=a;
                    strncpy(ans,sd,a);
                    ans[a]='\0';
                }
            }
            if(b<maxx)
            {
                b=maxx;
                strcpy(as,ans);
            }
            else if(b==maxx)
            {
                if(strcmp(as,ans)>0)
                    strcpy(as,ans);
            }
        }
        if(b)
        printf("%s\n",as);
        else
            printf("IDENTITY LOST\n");
    }
}

 

第二种 

#include <stdio.h>
#include <string.h>
#include<algorithm>
using namespace std;
const int maxx = 4001;
const int Max = 201;
char res[Max], s[maxx][Max];
int ne[Max],n;
void getNext(char *chs, int len)
{
    memset(ne, 0, sizeof(ne));
    for (int i = 1, j = 0; i < len; )
    {
        if (chs[i] == chs[j]) ne[i++] = ++j;
        else if (j > 0) j = ne[j-1];
        else i++;
    }
}
int getLogestPre(char *chs, int len)
{
    getNext(chs, len);
    for (int i = 1; i < n; i++)
    {
        char *p = s[i];
        int j = 0, tmp = 0;
        for (; *p && j < len; )
        {
            if (*p == chs[j])
            {
                p++, j++;
                tmp = max(tmp, j);
            }
            else if (j > 0) j = ne[j-1];
            else p++;
        }
        len = tmp;
    }
    return len;
}
int main()
{
    while (~scanf("%d", &n) && n)
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%s",s[i]);
        }
        int len = strlen(s[0]), ans = 0, pos = 0;
        for (int i = 0; i < len; i++)
        {
            int tmp = getLogestPre(s[0]+i, len-i);
            if (tmp >= ans)
            {
                if (tmp > ans)
                {
                    ans = tmp;
                    pos = i;
                }
                else
                {
                    bool smaller = true;
                    for (int t = 0; t < ans; t++)
                    {
                        if (s[0][pos+t] > s[0][i+t]) break;
                        else if (s[0][pos+t] < s[0][i+t])
                        {
                            smaller = false;
                            break;
                        }
                    }
                    if (smaller) pos = i;
                }
            }
        }
        if (ans)
        {
            for (int i = 0; i < ans; i++)
            {
                printf("%c",s[0][pos+i]);
            }
            printf("\n");
        }
        else
            printf("IDENTITY LOST\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值