HDU 1686 Oulipo (kmp)

题意:

       给定两个字符串a和b求a在中出现过几次

解法:

      kmp

AC代码如下:

/* Author: ACb0y Date: 2010年12月31日21:37:45 Type: KMP ProblemID: HDU 1686 Oulipo Result: 3367465 2010-12-27 21:33:16 Accepted 1686 93MS 1260K 817 B C++ ACb0y */ #include <iostream> using namespace std; char a[1000005]; char b[10005]; int next[10005]; int alen; int blen; void get_next() { int j = 0; int k = -1; next[0] = -1; while (j < blen) { if (k == -1 || b[j] == b[k]) { next[++j] = ++k; } else { k = next[k]; } } } int kmp_count() { int ans = 0; int i = 0, j = 0; get_next(); for (i = 0; i < alen; ++i) { while (j > 0 && a[i] != b[j]) { j = next[j]; } if (a[i] == b[j]) { ++j; } if (j == blen) { ++ans; j = next[j]; } } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int cas; scanf("%d", &cas); while (cas--) { scanf("%s%s", b, a); alen = strlen(a); blen = strlen(b); printf("%d/n", kmp_count()); } return 0; }

你可能感兴趣的:(HDU 1686 Oulipo (kmp))