poj3080

题目大意:

给你n个长度都为60的串,求出它们的最长公共子串,如果这个子串的长度 < 3则输出“no significant commonalities”,如果存在多个最长公共子串,则输出字典序最小的那一个。

解题思路:

直接枚举第一串的所有子串,然后与后面的所有串进行比较即可

代码如下:

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

const int len=60;

int main(int i,int j)
{
  int test;
  cin>>test;
  for(int t=1;t<=test;t++)
  {
    int n;
    cin>>n;
    char **DNA=new char*[n];
    for(int p=0;p<n;p++)
    {
      DNA[p]=new char[len+1];
      cin>>DNA[p];        
    }        
    char obj[len+1];
    int StrLen=0;
    int length=1;

    for(i=0;;i++)
    {
      char dna[len+1];
      int pi=i;
      if(pi+length>len)
      {
        length++;
        i=-1;
        if(length>len)
          break;
         continue;                 
      }           
      for(j=0;j<length;j++)
        dna[j]=DNA[0][pi++];
      dna[j]='\0';
      bool flag=true;
      for(int k=1;k<n;k++)
      {
        if(!strstr(DNA[k],dna))
        {
          flag=false;
          break;                       
        }        
      }  
      if(flag)
      {
        if(StrLen<length)
        {
          StrLen=length;
          strcpy(obj,dna);                 
        }        
        else if(StrLen==length)
        {
          if(strcmp(obj,dna)>0)
            strcpy(obj,dna);     
        }
      }
    }
    if(StrLen<3)
      cout<<"no significant commonalities"<<endl;
    else
      cout<<obj<<endl;
    delete DNA;
  }   
  return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值