FZU - 1926——填空 (KMP)
作者:互联网
voj题目链接:https://cn.vjudge.net/contest/292206#problem/D
FZU题目链接:http://acm.fzu.edu.cn/problem.php?pid=1926
思路:用KMP,否则会超时,视乎用cin也会超时。KMP处理时注意模式串上'_'符号的处理。
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
int t,m;
char str[1010][110],stw[1010][110];
int nextt[1010];
void init_kmp(int len)
{
memset(nextt,0,sizeof(int)*(len+5));
nextt[1]=0;
int i=1,j=2;
while(i<=len&&j<=len)
{
if(j==0||strcmp(stw[i],str[j])==0||strcmp(stw[i],"_")==0||strcmp(stw[j],"_")==0)
{
nextt[++i]=++j;
}
else
{
j=nextt[j];
}
}
for(int i=1;i<=len;i++)
printf("%d ",nextt[i]);
printf("\n");
}
int KMP(int lr,int lw)
{
int i=1,j=1;
while(i<=lr&&j<=lw)
{
if(j==0||strcmp(str[i],stw[j])==0||strcmp(stw[j],"_")==0)
{
++i,++j;
}
else
{
j=nextt[j];
}
}
return j>lw;
}
int main()
{
scanf("%d",&t);
int carry=1;
while(t--)
{
int lr=0;
do
{
++lr;
scanf("%s",str[lr]);
}
while(strcmp(str[lr],"@")!=0);
lr--;
scanf("%d",&m);
printf("Case %d:\n",carry++);
while(m--)
{
int lw=0;
do
{
++lw;
scanf("%s",&stw[lw]);
}
while(strcmp(stw[lw],"@")!=0);
lw--;
init_kmp(lw);
KMP(lr,lw)==1?printf("YES\n"):printf("NO\n");
}
}
}
标签:int,scanf,FZU,lw,while,lr,1926,KMP,填空 来源: https://blog.csdn.net/weixin_41380961/article/details/89005031