湘潭大学 ACM 1163 kmp 模板
链接http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1163
#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> using namespace std; char str[1200],cha[1200],pat[1200],next[1200]; int sta; void get_next( char *word ) { int len = strlen ( word ); int k = -1,j = 0; next[0] = -1; while ( j < len ) if ( k == -1 || str[k] == str[j] ) next[++j] = ++k; else k = next[k]; } int work( char *word ) { int len = strlen ( str ); int let = strlen ( word ); int i,k = 0; for ( i = sta; i < len; i++ ) { while ( str[i] != word[k] && next[k] != -1 ) k = next[k]; if ( str[i] == word[k] ) k++; if ( k == let ) return i+1; } return -1; } int main( ) { while ( scanf ( "%s" ,&str) != EOF ) { scanf ( "%s" ,&cha); int len = strlen ( cha ); cha[len++] = '*' ; int k = 0; bool fell = false ; sta = 0; for ( int i = 0; i < len; i++ ) { if ( cha[i] == '*' ) { if ( k == 0 ) continue ; pat[k] = '\0' ; k = 0; get_next( pat );sta = work( pat ); if ( sta < 0 ){ fell = true ; break ; } } else pat[k++] = cha[i]; } if ( !fell ) printf ( "yes\n" ); else printf ( "no\n" ); } return 0; } /* jfkadjfkdfjafjakjfaksfj ka*jf*ka */ |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步