HDU 2087剪花布条 KMP
生活随笔
收集整理的這篇文章主要介紹了
HDU 2087剪花布条 KMP
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
KMP算法入門題 ?哥第一次寫KMP慚愧啊,慚愧getnext寫得蛋痛,后面的KMP部分更蛋痛。改了N久
#include<stdio.h> #include<string.h> #define Max 1005 char str[Max+5] = {0},ch[Max+5] = {0}; int next[Max+5],count = 0,n; void getnext( ) {int i = 0,j = -1;next[0] = -1;while( ch[i] ){if( j == -1 || ch[i] == ch[j] )++i,++j,next[i] = j;elsej = next[j];}n = i; } void KMP( ) {int i = 0, j = 0;while( str[i] ){if( j == -1 || str[i] == ch[j] )++j,++i; while( str[i] && ch[j] && str[i] == ch[j] )++i,++j;if( j == n )++count,j = 0;elsej = next[j];} } int main( ) {while( scanf( "%s",str ),str[0] != '#' ){scanf( "%s",ch );n = count = 0;getnext( );KMP( );printf( "%d\n",count );}return 0; }
這是final版。。。改了好久啊。。。
轉(zhuǎn)載于:https://www.cnblogs.com/Lvsi/archive/2011/02/01/1948621.html
總結(jié)
以上是生活随笔為你收集整理的HDU 2087剪花布条 KMP的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Microsoft][ODBC SQL
- 下一篇: Win7系统下Vmware虚拟机无法使用