一开始没看清是每个里都得包含(只要枚举一个字符串里的所有字串就行了),结果就超时了。。。

匹配用的是KMP ,总算比较熟了,理解也更深了,还是得多练阿。。。

#include<cstdio>
#include<cstring>
using namespace std;
char m[110][110];
int f[110];
void fail(char *s)
{
  int i=0,j=-1;
  f[0]=-1;
  while(i<strlen(s))
  if(j==-1||s[i]==s[j])
  {
    j++;i++;
    if(s[i]==s[j]) f[i]=f[j];
    else f[i]=j;
  }
  else j=f[j];
}
int kmp( char *s1, char *s2)
{
  int i=0,j=0;
  fail(s2);
  int len1=strlen(s1),len2=strlen(s2);
  while(i<len1&&j<len2)
  {
    if(j==-1||s1[i]==s2[j])
    {
      j++;i++;
    }
    else j=f[j];
  }
  return j==strlen(s2);
}
int main()
{
  //freopen("a.txt","r",stdin);
  char ch[110],ch2[110];
  int len,len2;
  int nCase;scanf("%d",&nCase);
  while(nCase--)
  {
    int n;scanf("%d",&n);
    int maxLen=0,flag=1;
    for(int i=0;i<n;i++)
    {
      scanf("%s",m[i]);
    }
    for(int j=strlen(m[0])-1;j>=0;j--)
    {
      for(int k=j;k>=0;k--)
      {
        flag=1;
        len=0;len2=j-k+1;ch2[len2]='\0';
        for(int p=j;p>=k;p--)
        {
          ch[len++]=m[0][p];
          ch2[--len2]=m[0][p];
        }
        ch[len]='\0';
        for(int x=0;x<n;x++)
        {
          int a=!kmp(m[x],ch);int b=!kmp(m[x],ch2);
          if(a&&b)
          {flag=0;break;}
        }
        if(flag&&len>maxLen) maxLen=len;
      }
    }
  printf("%d\n",maxLen);
  }
  return 0;
}

 

posted on 2012-02-12 21:03  fakeAcmer  阅读(369)  评论(0编辑  收藏  举报