fzu 1481 环串
http://acm.fzu.edu.cn/problem.php?pid=1481
就是要匹配出模式串。
#include <stdio.h>
#include <string.h>
#define MAXLEN 205*3
char str[MAXLEN],pat[MAXLEN],t[MAXLEN];
int nextval[MAXLEN];
void getNextval(int len)
{
int j=0,k=-1;
nextval[0]=-1;
while(j<len)
{
if(k==-1 || str[j]==str[k])
{
++j;
++k;
if(str[j]==str[k]) nextval[j]=nextval[ nextval[k] ];
else nextval[j]=k;
}
else k=nextval[k];
}
}
bool kmp(int mlen,int patlen)
{
getNextval(patlen);
int i=0,j=0;
while(i<mlen && j<patlen)
{
if(j==-1 || str[i]==pat[j])
{
i++;
j++;
}
else j=nextval[j];
}
if(j>=patlen) return true;
else return false;
}
int main()
{
int n,count,len1,len2;
while(scanf("%s",str)!=EOF)
{
len1=strlen(str);
strcpy(t,str);
strcat(str,t);
strcat(str,t);
len1=len1+len1+len1;
scanf("%d",&n);
count=0;
while(n--)
{
scanf("%s",pat);
len2=strlen(pat);
strcpy(t,pat);
strcat(pat,t);
len2+=len2;
if(kmp(len1,len2)) count++;
}
printf("%d\n",count);
}
return 0;
}
浙公网安备 33010602011771号