E. Vasya and Binary String

链接

分析:

  对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可。

  然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为一个点。设f[i][j][k]表示消完区间[i,j]和这段区间后面k个元素最大值,其中k个元素的颜色与点j的颜色相同。

  转移:可以首先将j和后面k个元素消除,然后消除[i,j-1]。也可以枚举一个和j颜色相同的点m,然后分别先消除[m+1,r-1],剩下的区间就和后面k个连在一起了,再求出这段区间的答案。

  复杂度$O(n^4)$,但是跑不满,再记忆化一下,31ms。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
int n, cnt;
LL a[N], f[N], g[N][N][N];
char s[N];
struct Node { int size, col; } b[N]; void init() {
for (int i = ; i <= n; ++i)
for (int j = i; j <= n; ++j) f[j] = max(f[j], f[j - i] + a[i]);
int now = ;
for (int i = ; i <= n; ++i) {
if (s[i] == s[i - ]) now ++;
else cnt ++, b[cnt].size = now, b[cnt].col = s[i - ] - '', now = ;
}
++cnt; b[cnt].size = now, b[cnt].col = s[n] - '';
}
LL DP(int l,int r,int k) {
if (l == r) return f[b[l].size + k];
if (~g[l][r][k]) return g[l][r][k];
LL res = DP(l, r - , ) + f[b[r].size + k];
for (int i = l; i < r - ; ++i)
if (b[i].col == b[r].col) res = max(res, DP(i + , r - , ) + DP(l, i, b[r].size + k));
return g[l][r][k] = res;
}
int main() {
n = read();
scanf("%s", s + );
for (int i = ; i <= n; ++i) a[i] = read();
init();
memset(g, -, sizeof(g));
cout << DP(, cnt, );
return ;
}

最新文章

  1. Ajax实现简单下拉选项
  2. ImageSwitcher图片切换的简单用例
  3. Apache 的 httpd.conf 详解
  4. 安装PL/SQL Developer 遇到的问题及解决方法
  5. i3D的一篇Unity教程中的笔记
  6. 转:MyEclipse8.6插件安装方法
  7. PHP empty函数报错的解决办法
  8. the structure of the project (MVC)
  9. python-整理--pip whl命令
  10. sql嵌套批量更新
  11. MySQL比like语句更高效的写法locate position instr find_in_set
  12. OpenCV+OpenGL 双目立体视觉三维重建
  13. 理解Go Interface
  14. js字符串转json格式与json对象转字符串
  15. Python开发爬虫之BeautifulSoup解析网页篇:爬取安居客网站上北京二手房数据
  16. 3.4 自动测试初步《精通ASP.NET MVC 5》
  17. 【转】async &amp; await 的前世今生(Updated)
  18. cartographer 安装问题
  19. SQL PKG示例
  20. in 和 exist 区别 (转)

热门文章

  1. Forefront TMG 之 ISP 冗余传输链路(ISP-R)
  2. Java 系统学习梳理_【All】
  3. asp.net mvc文件下载
  4. CSS-定位属性
  5. 深入浅出SharePoint——配置List通过邮件来接收内容
  6. Python Frame
  7. gamit安装
  8. 1088. [SCOI2005]扫雷Mine【网格DP】
  9. [SDOI2010]Hide and Seek
  10. Windows启动控制台登录模式