题意:给定一篇文章和一些句子。询问句子是否在文章中出现。
kmp模板题
1 /* 2 kmp 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 #include<iostream> 9 #include<queue> 10 #include<map> 11 #include<stack> 12 #include<set> 13 #include<math.h> 14 using namespace std; 15 typedef long long int64; 16 //typedef __int64 int64; 17 typedef pair<int64,int64> PII; 18 #define MP(a,b) make_pair((a),(b)) 19 const int maxn = 1005; 20 const int maxm = 105; 21 const int inf = 0x7fffffff; 22 const double pi=acos(-1.0); 23 const double eps = 1e-8; 24 25 char text[ maxn ][ maxm ]; 26 int len_text; 27 char pat[ maxn ][ maxm ]; 28 int len_pat; 29 int next[ maxn ]; 30 31 void getnext(){ 32 int i,j; 33 next[ 0 ] = -1; 34 i = 0; 35 j = -1; 36 while( i<len_pat ){ 37 if( j==-1||strcmp( pat[i],pat[j] )==0||strcmp( pat[j],"_" )==0||strcmp( pat[i],"_" )==0 ){ 38 i ++ ; 39 j ++ ; 40 next[ i ] = j; 41 } 42 else 43 j = next[ j ]; 44 } 45 return ; 46 } 47 48 bool kmp( ){ 49 getnext(); 50 //for( int i=0;i<len_pat;i++ ){ 51 // printf("next[ %d ] = %d ",i,next[i]); 52 //} 53 int i,j; 54 i = 0; 55 j = 0; 56 while( i<len_text&&j<len_pat ){ 57 if( j==-1||strcmp( text[i],pat[j] )==0||strcmp(pat[j],"_")==0 ){ 58 i ++ ; 59 j ++ ; 60 } 61 else 62 j = next[j]; 63 if( j==len_pat ) return true; 64 } 65 return false; 66 } 67 68 int main(){ 69 int T; 70 scanf("%d",&T); 71 int Case = 1; 72 while( T-- ){ 73 len_text = 0; 74 while( 1 ){ 75 scanf("%s",text[ len_text ]); 76 if( strcmp( text[ len_text ],"@" )==0 ){ 77 break; 78 } 79 len_text ++ ; 80 }//input the passage 81 int m; 82 scanf("%d",&m); 83 printf("Case %d: ",Case++); 84 while( m-- ){ 85 len_pat = 0; 86 while( 1 ){ 87 scanf("%s",pat[ len_pat ]); 88 if( strcmp( pat[ len_pat ],"@" )==0 ){ 89 break; 90 } 91 len_pat ++ ; 92 } 93 bool flag = kmp(); 94 if( flag ) puts("YES"); 95 else puts("NO"); 96 } 97 } 98 return 0; 99 }