(字符串)简单/查找最长公共子序列

题目链接

POJ 3080 Blue Jeans

题目大意

就是找几个字符串中的最长的公共子序列,比较简单,可以暴力求解

思路

从第一个字符串中一次选取字符串(长度从1到60),然后与后面几组相比较

代码

#include <iostream>
#include <queue>
#include <fstream>
#include <string.h>
#include <string>
#include <stack>
using namespace std;

int main()
{
  //  ifstream fin("data.txt");
    char s[11][66];
    int N;
   // fin>>N;
   cin>>N;
    while(N--)
    {
        int n;
        //fin>>n;
        cin>>n;
        for(int i=0; i<n; i++)
            //fin>>s[i];
            cin>>s[i];
        char ans[66];
        int len = 1;
        int maxlen = 0;
        int start=0;
        while(1)
        {
            //从1选字符串截取的长度
            char tmp[66];
            int tt = start;
            if(tt+len>60)  //1选完了之后是2
            {
                len++;
                start=0;
                if(len>60)
                    break;
                continue;
            }
            int j;
            for(j=0; j<len; j++)
                tmp[j] = s[0][tt++];
            tmp[j]='\0';

            int ff=1;
            for(int k=1; k<n; k++) //将第一个串中的数据与之后的比较
            {
                if(!strstr(s[k],tmp))
                {
                    ff=0;
                    break;
                }
            }
            if(ff==1)
            {
                if(maxlen < len)
                {
                    maxlen = len;
                    strcpy(ans,tmp);
                }
                if(maxlen == len)
                {
                    if(strcmp(ans,tmp)>0)
                        strcpy(ans,tmp);
                }
            }
            start++;
        }
        if(maxlen<3)
            cout<<"no significant commonalities"<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值