zoukankan      html  css  js  c++  java
  • 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 
    ",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:
    ",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;
    }


  • 相关阅读:
    [转][c#]C# 二维数组到底该如何定义?
    [c++]筛法求素数
    USB驱动问题
    使用Ajax.dll前台调用后台方法及错误示例
    asp.net中前台javascript与后台C#交互
    visual stdio2010 生成的缓存文件
    jQuery.ajax概述[转]
    一种正向最小匹配的中文分词算法
    2010 .NET面试题整理之基础篇[转]
    Winform设计不规则窗体
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3304124.html
Copyright © 2011-2022 走看看