bzoj1710[Usaco2007 Open]Cheappal 廉价回文 dp
2017-06-17 11:53
246 查看
其实这题不算难,但是我dp比较弱。。。
一开始并没有发现性质,想着直接上,但是很难上。
设f[i][j]表示前i位做了j次操作的最小代价(保证前i位是回文串)
事实上这种就是xjb列式子,根本不管题目具体条件来。
回文串明显是左右相等啊,你就算列也列一个f[l,r]表示l,r是回文串的代价吧= =
性质:因为是回文串,所以添加其实就等于相等,因为在回文串的左边添加不就相当于在右边删除。
举个例子:abcb(样例)
左边添加bcb。成为bcbabcb,那么我们可以变成a,即他的回文是可以抵消的。
那么就很简单了。
经典区间dp。。。
dp[l][r]=min(dp[l][r-1]+cost[r],dp[l+1][r]+cost[l])
s[l]==s[r]的时候:
dp[l][r]=min(dp[l+1][r-1]);
一开始并没有发现性质,想着直接上,但是很难上。
设f[i][j]表示前i位做了j次操作的最小代价(保证前i位是回文串)
事实上这种就是xjb列式子,根本不管题目具体条件来。
回文串明显是左右相等啊,你就算列也列一个f[l,r]表示l,r是回文串的代价吧= =
性质:因为是回文串,所以添加其实就等于相等,因为在回文串的左边添加不就相当于在右边删除。
举个例子:abcb(样例)
左边添加bcb。成为bcbabcb,那么我们可以变成a,即他的回文是可以抵消的。
那么就很简单了。
经典区间dp。。。
dp[l][r]=min(dp[l][r-1]+cost[r],dp[l+1][r]+cost[l])
s[l]==s[r]的时候:
dp[l][r]=min(dp[l+1][r-1]);
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cmath> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std; int n, m; const int N = 1e5 + 5; int read() { int x = 0, f = 1; char ch = getchar(); while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x*f; } int dp[3005][3005]; char s ; int num ,cost[30]; inline int dfs(int l, int r) { if (l == r)return 0; if (~dp[l][r])return dp[l][r]; int res = 1e9; res = min(dfs(l + 1, r) + cost[num[l]], dfs(l, r - 1) + cost[num[r]]); if (num[l] == num[r])res = min(res, dfs(l + 1, r - 1)); return dp[l][r] = res; } int main() { m = read(), n = read(); scanf("%s", s+1); fo(i, 1, n)num[i] = s[i] - 'a' + 1; fo(i, 1, m) { char s1[5]; scanf("%s", s1 + 1); int x = read(), y = read(); cost[s1[1] - 'a' + 1] = min(x, y); } fo(i, 1, n) fo(j, 1, n)dp[i][j] = -1; printf("%d\n", dfs(1, n)); }
相关文章推荐
- 【BZOJ1710】[Usaco2007 Open]Cheappal 廉价回文【区间DP】
- BZOJ 1710: [Usaco2007 Open]Cheappal 廉价回文 区间dp
- bzoj 1710: [Usaco2007 Open]Cheappal 廉价回文【区间dp】
- BZOJ 1710: [Usaco2007 Open]Cheappal 廉价回文
- BZOJ1710: [Usaco2007 Open]Cheappal 廉价回文
- 动态规划 BZOJ1710 [Usaco2007 Open]Cheappal 廉价回文
- 1710: [Usaco2007 Open]Cheappal 廉价回文 (区间DP)
- bzoj1710【Usaco2007 Open】Cheappal 廉价回文
- bzoj 1710: [Usaco2007 Open]Cheappal 廉价回文 动态规划
- bzoj1710【Usaco2007 Open】Cheappal 便宜回文
- 1710: [Usaco2007 Open]Cheappal 廉价回文
- 【BZOJ】1710: [Usaco2007 Open]Cheappal 廉价回文
- 【bzoj1710/Usaco2007 Open】Cheappal 廉价回文——区间dp
- 1710: [Usaco2007 Open]Cheappal 廉价回文
- bzoj 1688: [Usaco2005 Open]Disease Manangement 疾病管理(状压DP)
- 2014.6.14模拟赛【bzoj1646】[Usaco2007 Open]Catch That Cow 抓住那只牛
- bzoj 3126: [Usaco2013 Open]Photo dp
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线(离散化+扫描线)
- [DP || 贪心 链表] BZOJ 4576 [Usaco2016 Open]262144 & BZOJ 4580 [Usaco2016 Open]248
- [BZOJ2442][Usaco2011 Open]修剪草坪 dp+单调队列优化