FZU-1926+KMP
题意:给定一篇文章和一些句子。询问句子是否在文章中出现。
kmp模板题
/* kmp */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<stack> #include<set> #include<math.h> using namespace std; typedef long long int64; //typedef __int64 int64; typedef pair<int64,int64> PII; #define MP(a,b) make_pair((a),(b)) const int maxn = 1005; const int maxm = 105; const int inf = 0x7fffffff; const double pi=acos(-1.0); const double eps = 1e-8; char text[ maxn ][ maxm ]; int len_text; char pat[ maxn ][ maxm ]; int len_pat; int next[ maxn ]; void getnext(){ int i,j; next[ 0 ] = -1; i = 0; j = -1; while( i<len_pat ){ if( j==-1||strcmp( pat[i],pat[j] )==0||strcmp( pat[j],"_" )==0||strcmp( pat[i],"_" )==0 ){ i ++ ; j ++ ; next[ i ] = j; } else j = next[ j ]; } return ; } bool kmp( ){ getnext(); //for( int i=0;i<len_pat;i++ ){ // printf("next[ %d ] = %d \n",i,next[i]); //} int i,j; i = 0; j = 0; while( i<len_text&&j<len_pat ){ if( j==-1||strcmp( text[i],pat[j] )==0||strcmp(pat[j],"_")==0 ){ i ++ ; j ++ ; } else j = next[j]; if( j==len_pat ) return true; } return false; } int main(){ int T; scanf("%d",&T); int Case = 1; while( T-- ){ len_text = 0; while( 1 ){ scanf("%s",text[ len_text ]); if( strcmp( text[ len_text ],"@" )==0 ){ break; } len_text ++ ; }//input the passage int m; scanf("%d",&m); printf("Case %d:\n",Case++); while( m-- ){ len_pat = 0; while( 1 ){ scanf("%s",pat[ len_pat ]); if( strcmp( pat[ len_pat ],"@" )==0 ){ break; } len_pat ++ ; } bool flag = kmp(); if( flag ) puts("YES"); else puts("NO"); } } return 0; }