Blue Jeans POJ 3080 (串)

本文介绍了一种使用暴力法查找多个字符串间最长且字典序最大的公共子串的方法,确保子串长度不少于3。通过遍历首个字符串的所有可能子串,并与其它字符串进行比较来实现。

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

题意很简单,找最长且字典序最大的公共子串,并且长度不能小于3


方法是暴力,找出第一个字符串的所有子串,依次与后面的字符串匹配。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<iostream>
using namespace std;
char a[15][65];
char temp[65];
char answer[65];
int len;
int k;
int Find;
int cases,n;
int main()
{
    cin>>cases;
    while(cases--)
    {
        Find=0;
        cin>>n;
        memset(answer,'\0',sizeof(answer));
        for(int i=1; i<=n; ++i)
            scanf("%s",a[i]);

        for(int i=1; i<=60; ++i)
        {
            for(int j=0; j<=60-i; ++j)
            {
                int flag=1;
                len=0;
                for(k=j;; k++)
                {
                    temp[len++] = a[1][k];
                    if(len == i)
                        break;
                }
                temp[len]='\0';
                //printf("%s\n",temp);
                for(k=2; k<=n; ++k)
                {
                    if(!strstr(a[k],temp))
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                {
                    Find=1;
                    if(strlen(answer)<strlen(temp))
                        strcpy(answer,temp);
                    else if(strcmp(answer,temp)>0)
                        strcpy(answer,temp);
                }
            }
        }

        if(!Find||strlen(answer)<3)
        {
            cout<<"no significant commonalities"<<endl;
        }
        else
        {
            printf("%s\n",answer);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值