【BZOJ1633】[Usaco2007 Feb]The Cow Lexicon 牛的詞典【DP】

【題目鏈接】

數據範圍都特別小,直接暴力就可以搞了。

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 605, maxl = 27, maxm = 305;

int n, m, dp[maxm], wlen[maxn];
char word[maxn][maxl], str[maxm];

inline int calc(int i, int id) {
	int res = 0;
	for(int j = 1; i <= m; i++) {
		if(str[i] == word[id][j]) j++;
		else res++;
		if(j == wlen[id] + 1) return res;
	}
	return -1;
}

int main() {
	scanf("%d%d%s", &n, &m, str + 1);
	for(int i = 1; i <= n; i++) scanf("%s", word[i] + 1), wlen[i] = strlen(word[i] + 1);

	for(int i = m; i >= 1; i--) {
		dp[i] = dp[i + 1] + 1;
		for(int j = 1; j <= n; j++) {
			int t = calc(i, j);
			if(t != -1) dp[i] = min(dp[i], dp[i + wlen[j] + t] + t);
		}
	}
	printf("%d\n", dp[1]);
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章