题目链接hdu3336

题目大意:给一串n长度的字符串str,问str所有前缀(包括本串)在str中的匹配次数。

解题思路:利用kmp爆搜得到一发TLE,参考kmp+dp 以及dp状态方程解释博客思路学会利用next数组的性质进行dp优化:next[i]表示不包含下标为i的字符(i下标之前的串)的最长前后缀相等长度,那么我们设dp[i]表示以第i个字符为结尾 (或者说长度为i)的前缀能够匹配到的前缀个数,思考一下:如果我们得到长度为i的前缀串,那么本身一定匹配1次,然后前面i-1长度的最长前后缀匹配1次(这里i-1的最长前后缀匹配次数要通过dp[next[i]]状态转移过来,因为next[i]的性质,然后通过这个长度取到匹配到的个数),最后两部分加起来就是dp[i]即长度为i的前缀匹配到的前缀个数,这样我们就能很容易得到 转移方程 : dp[i] = dp[next[i]] + 1


kmp+爆搜超时代码(思路局限到模板模拟导致)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
const int maxn = (int)2e5+5;
const int mod = 10007;

int prefix[maxn];

void get_prefix_table (string& pattern,int n) {
	int i = 0,len = -1;
	prefix[0] = -1;
	while (i < n) {
		if(len == -1 || pattern.at(i) == pattern.at(len)) {
			i++;
			len++;
			prefix[i] = len;
		}else {
			len = prefix[len];
		}
	}
	return ;
}

int kmp_search (string& tmp, string& pattern) {
	int i = 0,j = 0,n,m,ans = 0;
	n = tmp.size();
	m = pattern.size();
	get_prefix_table(pattern, m);
	while (i < n) {
		if(j == -1 || tmp.at(i) == tmp.at(j)) {
			i++;
			j++;
		}else {
			j = prefix[j];
		}
		if(j == m) {
			ans++;
			j = prefix[j];
		}
	}
	return ans;
}

int solve	(string& tmp) {
	int ans = 0;
	string pattern;
	for(int i = 1; i <= tmp.size(); i++) {
		pattern = tmp.substr(0,i);
		ans = (ans + kmp_search(tmp, pattern))%mod;
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	int T,n;
	string str;
	cin >> T;
	while (T--) {
		cin >> n >> str;
		cout << solve(str) << '\n';
	}
	return 0;
}

AC代码

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
const int maxn = (int)2e5+5;
const int mod = 10007;

int prefix[maxn],dp[maxn];
string pattern;

void get_prefix_table() {
	int i = 0,len = -1,n = pattern.size();
	prefix[0] = -1;
	while (i < n) {
		if(len == -1 || pattern.at(i) == pattern.at(len)) {
			i++;
			len++;
			prefix[i] = len;
		}else {
			len = prefix[len];
		}
	}
	return ;
}

int main() {
	ios::sync_with_stdio(false);
	int T,n,sum;
	cin >> T;
	while (T--) {
		cin >> n >> pattern;
		get_prefix_table();
		memset(dp,0,sizeof(dp));
		dp[0] = 0;
		sum = 0;
		for (int i = 1; i <= n; i++) {
			dp[i] = (dp[prefix[i]] + 1) % mod;
			sum = (sum + dp[i]) % mod;
		}
		cout << sum << '\n';
	}
	return 0;
}