hdu2594-Simpsons’ Hidden Talents

来源:互联网 发布:网络数据分析邱均平 编辑:程序博客网 时间:2024/05/26 05:09

Simpsons’ Hidden Talents

上面的KMP模板是套用别人的

// File Name: hdu2594.cpp// Author: bo_jwolf// Created Time: 2013年04月30日 星期二 18:42:23#include<vector>#include<list>#include<map>#include<set>#include<deque>#include<stack>#include<bitset>#include<algorithm>#include<functional>#include<numeric>#include<utility>#include<sstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<ctime>using namespace std;const int maxn = 50005 ;int next[ maxn ] , ex[ maxn ] ;char str1[ maxn ] , str2[ maxn ] ;void getNext(char *p){    int j,k;    j=0;    k=-1;    int len=strlen(p);    next[0]=-1;    while(j<len)    {        if(k==-1||p[j]==p[k])        {            j++;            k++;            next[j]=k;        }        else k=next[k];    }}void get_ex(char *p,char *q){    int len1=strlen(p);    int len2=strlen(q);    getNext(p);    int i,k;    ex[0]=0;    i=0;    k=0;    while(i<len2)    {        if(k==-1||p[k]==q[i])        {            i++;            k++;            ex[i]=k;        }        else k=next[k];    }}int main(){while( scanf( "%s%s" , str1 , str2 ) != EOF ){get_ex( str1 ,str2 );int n = strlen( str2 );if( ex[ n ] == 0 )printf( "0\n" );else{for( int i = 0 ; i < ex[ n ] ; i++ )printf( "%c" , str1[ i ] ) ;printf( " %d\n",ex[ n ] );}}return 0;}


原创粉丝点击