kmp 填空

来源:互联网 发布:网络平台运营方案模板 编辑:程序博客网 时间:2024/04/28 00:20

http://acm.fzu.edu.cn/problem.php?pid=1926


input

1

much of the language used to describe monetary policy such as steering the economy to a soft landing or a touch on the brakes makes it sound like a precise science nothing could be further from the truth @

3

much of the language _ _ describe monetary policy @

steering the economy to a _ landing @

much of the _ describe monetary @


一串字符,以@结尾,作为母串.

接下来n各子串,也已@作为结尾,子串中包含 _ 代表需要填写的单词,问,能不能匹配.


#include<stdio.h>#include<string.h>#include<string>using namespace std;int T,n,len_s,len_ss,pos,next[1111];char str[1111];string s[1111],ss[1111];void getnext(){    memset(next,0,sizeof(next));    int i=0,j=-1;    next[i]=j;    while(i!=len_ss){        if(j==-1||ss[i]==ss[j]||ss[i]=="_"||ss[j]=="_"){ //在这里加入判断就好了            next[++i]=++j;        }        else{            j=next[j];        }    }}bool kmp(){    int i=0,j=0;    while(i!=len_s&&j!=len_ss){        if(j==-1||s[i]==ss[j]||ss[j]=="_"){  //在这里加入判断            ++i;j++;            if(j==len_ss){                return true;            }        }        else{            j=next[j];        }    }    return false;}int main(){    scanf("%d",&T);    int cas=1;    while(T--){        pos=0;        printf("Case %d:\n",cas++);        while(1){            scanf("%s",str);            if(strcmp(str,"@")==0){                break;            }            s[pos++]=string(str);        }        len_s=pos;        scanf("%d",&n);        while(n--){            pos=0;            while(1){                scanf("%s",str);                if(strcmp(str,"@")==0){                    break;                }                ss[pos++]=string(str);            }            len_ss=pos;            getnext();            if(kmp()){                printf("YES\n");            }            else{                printf("NO\n");            }        }    }    return 0;}


原创粉丝点击