秋招快有着落啦,十一月中去北京区赛膜拜众神。
哎,好长一段时间没有刷过,重头拾起,最近得专题是字符串。
Trie前一排又敲了一遍,KMP今天敲了一下。
题目一大堆废话,实际就是判断模式串出现得次数,我是对着算法导论伪代码敲得,一次AC,真得很水。
/***********************************************************
> OS : Linux 3.13.0-24-generic (Mint-17)
> Author : yaolong
> Mail : dengyaolong@yeah.net
> Time : 2014年09月24日 星期三 15时31分51秒
**********************************************************/
#include
#include
#include
#include
using namespace std; int next[12345]; char p[12345]; char T[1234567]; void build_next ( int m ) { next[1] = 0; int k = 0; for ( int q = 2; q <= m; q++ ) { while ( k > 0 && p[k + 1] != p[q] ) { k = next[k]; } if ( p[k + 1] == p[q] ) { k = k + 1; } next[q] = k; } } int kmp ( int n, int m ) { build_next ( m ); int q = 0; int res = 0; for ( int i = 1; i <= n; i++ ) { while ( q > 0 && p[q + 1] != T[i] ) { q = next[q]; } if ( p[q + 1] == T[i] ) { q = q + 1; } if ( q == m ) { //cout << "Here" << i-m<<"q"; ++res; q = next[q]; //cout<