Bzoj 1055 玩具取名(区间DP)
题面
题解
字符很麻烦,不妨用数字代替(比如1代表'W')
const char c[5] = {0, 'W', 'I', 'N', 'G'};
接着,像这种两个子串可以合并成另一个子串的题可以考虑区间$DP$
设$bool$数组$f_{i,j,k}$表示区间$[l,r]$能否合成单个字符$c_k$
于是就可以套区间$DP$的板子了
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::sort; using std::swap;
using std::unique; using std::lower_bound;
using std::map;
typedef long long ll;
template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
}
const int Len = 2e2 + 10, _ = 5;
const char c[5] = {0, 'W', 'I', 'N', 'G'};
char s[Len]; bool p, f[Len][Len][_];
int num[_], list[Len][_], tot, len;
int check(char x) { for(int i = 1; i <= 4; ++i) if(x == c[i]) return i; }
int main () {
for(int i = 1; i <= 4; ++i) read(num[i]);
for(int i = 1; i <= 4; ++i)
for(int j = 1; j <= num[i]; ++j) {
char ss[5]; scanf("%s", ss);
list[++tot][0] = i;
list[tot][1] = check(ss[0]);
list[tot][2] = check(ss[1]);
}
scanf("%s", s + 1), len = strlen(s + 1);
for(int i = 1; i <= len; ++i) f[i][i][check(s[i])] = true;
for(int k = 1; k <= len; ++k)
for(int i = 1; i + k <= len; ++i) {
int j = i + k;
for(int p = i; p < j; ++p)
for(int l = 1; l <= tot; ++l)
if(f[i][p][list[l][1]] && f[p + 1][j][list[l][2]])
f[i][j][list[l][0]] = true;
}
for(int i = 1; i <= 4; ++i)
if(f[1][len][i]) putchar(c[i]), p = true;
if(!p) printf("The name is wrong!");
puts("");
return 0;
}
Bzoj 1055 玩具取名(区间DP)的更多相关文章
- BZOJ 1055 玩具取名(区间DP)
很显然的区间DP,定义dp[i][j][k], 如果dp[i][j][k]=1表示字符串[i,j]可以组成k字符. # include <cstdio> # include <cst ...
- Bzoj 1055: [HAOI2008]玩具取名 (区间DP)
Bzoj 1055: [HAOI2008]玩具取名 (区间DP) 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间动态规划和可 ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- 【BZOJ-1055】玩具取名 区间DP
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1560 Solved: 907[Submit][Statu ...
- BZOJ 1055 玩具取名
Description 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使 ...
- bzoj1055玩具取名——区间DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间DP,注意初始化!! 因为没记忆化,TLE了一晚上,区间DP尤其要注意不重复递归! ...
- bzoj1055: [HAOI2008]玩具取名(dp)
1055: [HAOI2008]玩具取名 题目:传送门 简要题意: 就是固定四个字母,给出这四个字母分别可以由哪两个字母组成,然后在给你一个字符串,要求把这个字符串还原成原始的四个字母的其中一个. 题 ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- BZOJ 2933([Poi1999]地图-区间Dp)
2933: [Poi1999]地图 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 7 Solved: 7 [ Submit][ Status] ...
随机推荐
- Java——Read/convert an InputStream to a String
获取 InputStream 并将其转换为String的简单方法. 添加commons-io-2.4.jar import java.io.IOException; import java.io.In ...
- [uva11997]k个最小和
一个k*k的矩阵,每行选取一个数相加则得到一个和,求最小的前k个和. k<=750 已知前m行最小的前k个和d[1]…d[k],则前m+1行最小的前k个和都必定是d[i](i<=k)+a[ ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- Fiddler--的一些使用技巧
1.Filters请求与响应的会话过滤 请求会话列表中存在上百个请求,怎么过滤想要的,可以启用 Fiddler Filters强大的过滤机制,还可以依据正则来过滤,如: REGEX:(?insx). ...
- 基于 Arduino 开发板,这款插座是可编程且开源的
基于 Arduino 开发板,这款插座是可编程且开源的 https://www.oschina.net/news/74861/open-source-socket https://github.com ...
- glom模块的使用(一)
glom模块的使用 简单说下glom模块主要是处理结构化数据用的,安装简单pip install glom即可,下面就glom的方法参数做例子讲解. glom 和模块同名的glom方法使用方法: .g ...
- 设计模式之笔记--单例模式(Singleton)
单例模式(Singleton) 定义 单例模式(Singleton),保证一个类仅有一个实例,并提供一个访问它的全局访问点. 类图 描述 类Singleton的构造函数的修饰符为private,防止用 ...
- 004_ssh连接慢的问题的解决?
<1>群中同学遇到的问题,我之前在uuwatch也遇到了同样的问题? 问个问题师兄们 突然之间 公司服务器连接很慢 连一个shell需要10几秒钟 服务器就在公司全是内网服务器, 我也不知 ...
- Ruby-Clamp
require "clamp" class ClampTest < Clamp::Command # 1.命令行的参数使用主要分两类,一种是参数名称后面带参数值的方式, #我 ...
- 虚拟机安装苹果系统 VMware 12安装Mac OS X 10.10
工具/原料 VMware Workstation Pro 12 (这个可以自己下载,并激活,你懂得) Unlocker 207 (链接:http://pan.baidu.com/s/1i43obDb ...