poj 3450 KMP+暴力枚举(类似于3080)

类似于3080 稍作改动,即可AC。。

#include<iostream>
using namespace std;
int next[205];
char str[205];
char ans[205];
char s[5050][205];
int h;
int le;
void get_next(char *T,int len)
{
  int j=0,k=-1;
  next[0]=-1;
  while(j<len)
  {
    if(k==-1||T[j]==T[k])
    {
      next[j+1]=k+1;
      j++;k++;
    }
    else
    k=next[k];
  }
  //for(int i=0;i<len;i++)
  //printf("%d ",next[i]);
}
bool find(char *str)
{
  int len=strlen(str);
    int i=0,j=0;
    while(i<=le&&j<len)
    {
      if(j==-1||s[h][i]==str[j])
      {
        j++;i++;
      }
      else
      j=next[j];
    }
    if(j==len) return true;
    else  return false;
}
int main()
{
  int k,l,n;
    while(scanf("%d",&n)!=EOF&&n)
    {
    for(int i=0;i<n;i++)
    scanf("%s",s[i]);
    le=strlen(s[0]);
    int t_len=0;
    strcpy(ans,"\0");
    for(int i=1;i<=le;i++)
    {
      for(int j=0;j<=le-i;j++)
      {
        for(k=j,l=0;k<i+j;k++,l++)
        str[l]=s[0][k];
        str[l]='\0';
        get_next(str,i);
        //for(int m=0;m<i;m++)
        //printf("%d ",next[m]);
        //system("pause");
        for(h=1;h<n;h++)
        if(!find(str))  break;
        if(h==n)
        {
          if(t_len<i)
          {
            t_len=i;
            strcpy(ans,str);
          }
          else if(t_len==i)
          {
            if(strcmp(ans,str)>0)
            {
              strcpy(ans,str);
            }
          }
        }
        //else
        //printf("No\n");
      }
    }
    if(ans[0]!='\0')
    printf("%s\n",ans);
    else
    printf("IDENTITY LOST\n");
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值