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;
}

你可能感兴趣的:(POJ Blue Jeans(3080)-STL&&KMP)