区间DP
顾名思义,就是用区间作为状态,dp[l][r]表示处理区间[l, r]最少/多怎么怎么样。
例题
G
状态
表示[i,j]区间能否从‘W’转移过来
表示[i,j]区间能否从‘I’转移过来
表示[i,j]区间能否从‘N’转移过来
表示[i,j]区间能否从‘G’转移过来
状转
对于每一个,都是
for(int len = 2; len <= n; len++){
for(int i = 1; i + len - 1 <= n; i++){
int j = i + len - 1;
for(int k = i; k < j; k++){
for(int c = 0; c <= 3; c++){
for(int c1 = 0; c1 <= 3; c1++){
for(int c2 = 0; c2 <= 3; c2++){
if(f[c][c1][c2] == 1 && dp[i][k][c1] == 1 && dp[k + 1][j][c2] == 1){
dp[i][j][c] = 1;
}
}
}
}
}
}
}因为如果前半段可以由转过来,后半段可以由转过来,,那么整段可以由转移过来
Code
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
AK N = 205;
AK M = 100005;
AK INF = 1e18;
int a[10];
map<int, int> mp;
string s;
bool dp[N][N][4], f[4][4][4];
void work(){
//please write your code on here
cin >> a[0] >> a[1] >> a[2] >> a[3];
mp['W'] = 0, mp['I'] = 1, mp['N'] = 2, mp['G'] = 3;
for(int i = 0; i <= 3; i++){
for(int j = 1; j <= a[i]; j++){
char c1, c2;
cin >> c1 >> c2;
f[i][mp[c1]][mp[c2]] = 1;
}
}
cin >> s;
int n = s.size();
s = " " + s;
for(int i = 1; i <= n; i++){
dp[i][i][mp[s[i]]] = 1;
}
for(int len = 2; len <= n; len++){
for(int i = 1; i + len - 1 <= n; i++){
int j = i + len - 1;
for(int k = i; k < j; k++){
for(int c = 0; c <= 3; c++){
for(int c1 = 0; c1 <= 3; c1++){
for(int c2 = 0; c2 <= 3; c2++){
if(f[c][c1][c2] == 1 && dp[i][k][c1] == 1 && dp[k + 1][j][c2] == 1){
dp[i][j][c] = 1;
}
}
}
}
}
}
}
bool f1 = 0;
if(dp[1][n][0]){f1 = 1;cout << "W";}
if(dp[1][n][1]){f1 = 1;cout << "I";}
if(dp[1][n][2]){f1 = 1;cout << "N";}
if(dp[1][n][3]){f1 = 1;cout << "G";}
if(!f1){
cout << "The name is wrong!";
}
}
problem T1(){
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}H
状态
表示[i,j]区间的最少操作次数
状转
如果那么dp[i][j] = dp[i + 1][j - 1];
同时dp[i][j] = min(dp[i][j], min(dp[i + 1][j], dp[i][j - 1]) + 1);
然后又一个中转点k
dp[i][j]可以由dp[i][k] + dp[k + 1][j]转移
Code
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
AK N = 1005;
AK M = 1005;
AK INF = 1e18;
int dp[N][N], n, s[N];
void work(){
//please write your code on here
cin >> n;
for(int i = 1; i <= n; i++){
cin >> s[i];
}
memset(dp, 0x3f, sizeof(dp));
for(int i = 1; i <= n; i++){
dp[i][i] = 1;
(i != n ? dp[i][i + 1] = (s[i + 1] != s[i]) + 1: 1);
}
for(int len = 2; len < n; len++){
for(int i = 1; i <= n - len; i++){
int j = i + len;
if(s[i] == s[j]){
dp[i][j] = dp[i + 1][j - 1];
}
dp[i][j] = min(dp[i][j], min(dp[i + 1][j], dp[i][j - 1]) + 1);
for(int k = i; k < j; k++){
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
}
}
}
cout << dp[1][n];
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}