POJ Blue Jeans(3080)-STL&&KMP

Input

Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
  • A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
  • m lines each containing a single base sequence consisting of 60 bases.

Output

For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

题意: TT组测试数据, 每组n个字符串,  寻找所有串中最长的公共序列 ;

解(1):   暴力搜, 有STL代码方便了很多.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>`
#include <algorithm>
#include <cctype>
#include <set>
#include <vector>
#define INF 0x7fffffff
#define eps (1e-9)
#define clearto(s,x) memset(s,x,sizeof(s))
using namespace std;
int n,m,tot=0;
int letMeAC()
{
    int i,k,t,l;
    string s[15];       string smax="";
    for(i=0;i<n;i++)    cin>>s[i];
    for(i=0;i<s[0].size();i++)
      for(l=3;l+i<=s[0].size();l++)
      {
         string stp= s[0].substr(i,l);
         int ok= 1;
         for(t=1;t< n;t++)
         if(s[t].find(stp)==string::npos)
         {    ok=0;         break;    }
         if(ok==0)          continue;
         if((l==smax.size())&&(stp<smax))    smax =stp;
         else  if(l> smax.size())            smax =stp;
      }
    if(smax.size()<3)     printf("no significant commonalities\n");
    else                  printf("%s\n",smax.c_str() );
}
int main()
{
    freopen("D:\data.txt","r",stdin);
    int TT,i,k;
    scanf("%d",&TT);
    while(TT--){
       scanf("%d",&n);       letMeAC();
    }
    return 0;
}

解(2) :应用KMP算法,参考网上题解又交了个,时间优化很多:


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>`
#include <algorithm>
#include <cctype>
#include <set>
#include <vector>
#define INF 0x7fffffff
#define eps (1e-9)
#define clearto(s,x) memset(s,x,sizeof(s))
using namespace std;
int n,m,tot=0;
int kmp(char st[],char p[])
{
    int next[100];    next[0]= -1;    next[1]= 0;
    int i =0,k,t, ls =strlen(st), lp =strlen(p);
    while(++i <lp-1){
        t =next[i];
        while(t!=-1 && p[i]!=p[t])   t =next[t];
        next[i+1] = t+1 ;
    }
    i = t =0;
    while(i<ls&&t<lp)
    {  if(t==-1 || st[i]==p[t]){ i++; t++; }  else  t =next[t];    }
    return (t==lp);
}
int main()
{
    //freopen("D:\data.txt","r",stdin);
    int TT,i,k,t; char stp[100],ans[100];     char s[20][100];
    scanf("%d",&TT);
    while(TT--){
       scanf("%d",&n);
       for(i=0;i<n;i++)       scanf("%s",s[i]);
       int flag=0;
       int len,ll =strlen(s[0]);
       for(len =ll;len>2&&flag==0; len--)      //从最长的子串开始枚举
       {
           strcpy(ans,"ZZ");
           for(i=0;i+len<=ll;i++)              //在len长度下顺序枚举s[0]
           {
               for(t=0;t<len;t++)  stp[t]=s[0][i+t];     stp[t]='\0';
               for(k=1;k < n;k++)  if(kmp(s[k],stp)==0)  break;
               if(k==n)
               {  if(strcmp(ans,stp)>0)   strcpy(ans,stp);   flag=1;   }
           }
       }
       if(flag==1)   printf("%s\n",ans);
       else printf ("no significant commonalities\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值