标签:acm
#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <algorithm> using namespace std; #define maxn 400000 + 10 int len, next[maxn], cnt = 0; int L; string s1, s; void getnext() { int i = 0; int j = -1; next[0] = -1; while(i < len) { if(j == -1 || s[i] == s[j]) { i++, j++; next[i] = j; } else j = next[j]; } } void kmp() { int i = 0; int j = 0; cnt = 0; getnext(); while(i < L) { if(j == -1 || s1[i] == s1[j]) { i++; j++; } else j = next[j]; if(j == len) { cnt++; j = next[j]; } } } int main() { cin>>s>>s1; len = s.size(); L = s1.size(); kmp(); cout<<cnt<<endl; system("pause"); return 0; }
标签:acm
原文地址:http://blog.csdn.net/dojintian/article/details/44982679