poj 3080-Blue Jeans(暴力KMP)

传送门:

poj 3080

题意:

给出一系列长度为60的字符串,让求出它们的最大的公共子序列

题解:

1、既然是公共子序列,那么在其中一个数据里面可能会有一个子串是满足条件的,那么我们可以将其中的一个串作为基底(这里我是把第一个串作为基底)进行个计算即可。实现这一步操作我用到了C++里面的substr()函数,这个函数的作用是将string中的从第一个变量位置开始的字符到长度为第二个变量的字符复制到另一个string变量中。

参考博客:https://blog.csdn.net/sunshihua12829/article/details/50484966

这样我们就可以设置两个for循环实现遍历所有的情况,同时定义一个string变量用于与后面的所有字符串进行比较,判断这个子串是否成立:

成立条件

       1:长度比原本的串的长度长

       2:如果说长度与原本的串的长度相同的话,取字典序小的串作为输出结果

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int N,n;
string num[15];
string num_puts;
int num_puts_number;
int nexxt[100];


void find_next(string word)
{
    nexxt[0]=-1;
    int k=-1;
    int j=0;
    while(j<word.length())
    {
        if(k==-1||word[k]==word[j])
        {
            ++k;
            ++j;
            if(word[k]!=word[j])
                nexxt[j]=k;
            else
                nexxt[j]=nexxt[k];
        }
        else
        {
            k=nexxt[k];
        }
    }
}
bool kmp(string word,string num)
{
    memset(nexxt,0,sizeof(nexxt));
    find_next(word);
    int i=0;
    int j=0;
    while(j<num.length())
    {
        if(i==-1||word[i]==num[j])
        {
            i++;
            j++;
        }
        else
        {
            i=nexxt[i];
        }

        if(i==word.length())
        {
            return true;
        }
    }
    return false;
}
int main()
{
    cin>>N;
    while(N--)
    {
        cin>>n;
        int nnn=0;
        for(int i=1; i<=n; ++i)
        {
            cin>>num[i];
        }
        //输入数据。
        int len=num[1].length();//计算第一个串的长度
        num_puts="";
        for(int i=0; i<60; ++i)
        {
            for(int j=1; i+j-1<60; ++j)
            {
                string word=num[1].substr(i,j);
                int number=0;
                for(int k=2; k<=n; ++k)
                {
                    if(kmp(word,num[k]))
                    {
                        ++number;
                    }
                    else
                    {
                        break;
                    }
                }
                if(number==n-1)
                {
                    if(word.length()>num_puts.length())
                    {
                        num_puts=word;
                    }
                    else if(word.length()==num_puts.length())
                    {
                        if(word<num_puts)
                        {
                            num_puts=word;
                        }
                    }
                }
            }
        }
        if(num_puts.length()<3)
        {
            printf("no significant commonalities\n");
        }
        else
        {
            cout<<num_puts<<endl;
        }
    }
    return 0;
}
/*


3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

5555
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA


*/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值