CF 477 C Dreamoon and Strings (DP)

題目: LINK

dp[i][j] 表示前i個字母裏面去除j個字母后最多的不重複的p串的數量.
有兩種情況,要麼選取1~i中最後一個和p一樣的串,要麼不選取,dp[i][j] = max(dp[i-1][j], dp[ii][jj]+1), ii爲匹配完p後在原串中的位置,jj爲j-(匹配p過程中刪去字母的數量);

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std; 
#define INF 1000000000
//typedef __int64 LL; 
#define N 2011
#define M 511
char str1[N], str2[N]; 
int dp[N][N]; 

int main() 
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin); 
#endif // ONLINE_JUDGE
	scanf("%s%s", str1+1, str2+1); 
	int len1 = strlen(str1+1); 
	int len2 = strlen(str2+1); 
	for(int i = 1; i <= len1; i ++) {
		int ii = i, jj = len2; 
		while(ii >= 1 && jj >= 1 ) {
			if(str1[ii] == str2[jj]) jj--; 
			ii --; 
		}
		if(jj == 0) {
			int dif = i - ii - len2; 
			for(int th = 0; th <= ii; th ++) {
				dp[i][dif + th] = dp[ii][th] + 1; 
			}
		}
		for(int th = 0; th < i; th ++) {
			dp[i][th] = max(dp[i][th], dp[i-1][th]); 
		}
	}
	for(int i = 0; i <= len1; i ++) {
		printf("%d ", dp[len1][i]); 
	}
	return 0; 
}


發佈了125 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章