Codeforces 803E--Roma and Poker (DP)
原题链接:http://codeforces.com/problemset/problem/803/E
题意:给一个n长度的字符串,其中'?'可以替换成'D'、'W'、'L'中的任意一种,'D'等价于0, 'W'等价于1、'L'等价于-1。输出所有'?'被替换掉后,W和L的数目之差为k,且任意一个[1, i]的子串中W和L数目之差不能等于k。
思路:用DP做。定义bool dp[i][j]代表前i个字符W和L数目之差为j, -k<=j<=k(在数组中范围为[0, 2*k]),那么当str[i]为'D'时dp[i][j]转移到dp[i-1][j], 为'W'时dp[i][j]转移到dp[i-1][j+1], str[i]为'D'时dp[i][j]转移到dp[i-1][j-1], 初始值dp[0][0]为true。
接着用一遍dfs倒推求结果,注意字符加的位置。
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
using namespace std;
int dp[][];
int n,k;
string str;
void change(int i, int L, int D, int W){
for(int j=;j<*k;j++){
if(dp[i-][j]){
if(D) dp[i][j]=;
if(L){
if(i!=n&&j-==)//[1, i]子串中W和L数目之差不能等于k
continue;
dp[i][j-]=;
}
if(W){
if(i!=n&&j+==*k)
continue;
dp[i][j+]=;
}
}
}
return;
}
string ss;
//int t=0;
bool res(int i, int j, string ans){
//t++;
if(i==&&j==k){
ss=ans;
return ;
}
if(str[i-]!='?'){
if(str[i-]=='D') return res(i-, j, 'D'+ans);
if(str[i-]=='W') return res(i-, j-, 'W'+ans);
if(str[i-]=='L') return res(i-, j+, 'L'+ans);
}
else
{
if(dp[i-][j]&&res(i-, j, 'D'+ans)) return ;
if(dp[i-][j-]&&res(i-, j-, 'W'+ans)) return ;
if(dp[i-][j+]&&res(i-, j+, 'L'+ans)) return ;
} return ;
}
int main()
{
while(cin>>n>>k)
{
memset(dp, , sizeof(dp));
dp[][k]=;
cin>>str;
if(str[n-]=='D'){
cout<<"NO"<<endl;
continue;
}
for(int i=;i<=n;i++){
if(str[i-]=='?')
change(i, , , );
else if(str[i-]=='D')
change(i, , , );
else if(str[i-]=='W')
change(i, , , );
else
change(i, , , );
}
string ans;
if(dp[n][]){
res(n, , ans);
cout<<ss<<endl;
}
else if(dp[n][*k]){
res(n, *k, ans);
cout<<ss<<endl;
}
else
cout<<"NO"<<endl;
//cout<<t<<endl;
}
return ;
}
这代码调了我好久啊QAQ,感觉自己真菜
Codeforces 803E--Roma and Poker (DP)的更多相关文章
- Educational Codeforces Round 20 E - Roma and Poker(dp)
传送门 题意 Roma在玩一个游戏,一共玩了n局,赢则bourle+1,输则bourle-1,Roma将会在以下情况中退出 1.他赢了k个bourle 2.他输了k个bourle 现在给出一个字符串 ...
- Codeforces 803E - Roma and Poker
http://codeforces.com/problemset/problem/803/E E. Roma and Poker time limit per test 2 se ...
- CodeForces - 710E Generate a String (dp)
题意:构造一个由a组成的串,如果插入或删除一个a,花费时间x,如果使当前串长度加倍,花费时间y,问要构造一个长度为n的串,最少花费多长时间. 分析:dp[i]---构造长度为i的串需要花费的最短时间. ...
- Educational Codeforces Round 51 D. Bicolorings(dp)
https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...
- Codeforces 536D - Tavas in Kansas(dp)
Codeforces 题目传送门 & 洛谷题目传送门 其实这题本该 2019 年 12 月就 AC 的(详情请见 ycx 发此题题解的时间),然鹅鸽到了现在-- 首先以 \(s,t\) 分别为 ...
- Codeforces 295D - Greg and Caves(dp)
题意: 给出一个 \(n \times m\) 的矩阵,需对其进行黑白染色,使得以下条件成立: 存在区间 \([l,r]\)(\(1\leq l\leq r\leq n\)),使得第 \(l,l+1, ...
- Codeforces 467C George and Job(DP)
题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...
- Codeforces A ACM (ACronym Maker) (dp)
http://codeforces.com/gym/100650 概要:给出一个缩写,和一些单词,从单词中按顺序选一些字母作为缩写,问方案数. 限制:某些单词要忽略,每个单词至少要选一个字母. dp[ ...
- codeforces 813 D. Two Melodies(dp)
题目链接:http://codeforces.com/contest/813/problem/D 题意:求两个不相交的子集长度之和最大是多少,能放入同一子集的条件是首先顺序不能变,然后每一个相邻的要么 ...
随机推荐
- 纯文本编辑语言markdown
Markdown的主要目的是生成可以复制到网页或写入平台的HTML代码.但你不必那样使用它.Markdown也可以作为强大记笔记的基础,许多Markdown编辑可以将您的写作导出为其他格式,如Word ...
- 《图解设计模式》读书笔记1-1 Iterator模式
目录 迭代器模式的类图 类图的解释 迭代器模式的代码 解释 原因 思想 迭代器模式的类图 类图的解释 名称 说明 Aggregate 集合接口,有提供迭代器的方法 Iterator 迭代器接口,提供迭 ...
- delphi 按钮 2 行
http://bbs.csdn.net/topics/390230792 回复于: 2015-06-01 21:11:02 最简单的办法:------------------------以下是转载的, ...
- Socket错误详解及处理方法
例如错误代码10061, 说明服务器已经找到,但连接被服务器拒绝, 连接失败原因可能是: 端口号设置错误: 2.服务器没有处于监听状态 (即ServerSocket –>Active=true) ...
- Kestrel web server implementation in ASP.NET Core
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore1x&view ...
- 解读:nginx的一个神秘配置worker_cpu_affinity
今天在查看nginx的相关知识的时候发现了一个nginx之前不认识的配置:worker_cpu_affinity. nginx默认是没有开启利用多核cpu的配置的.需要通过增加worker_cpu_a ...
- HTML--JS 随机背景色
<html> <head> <title>背景随机变色</title> <script type="text/javascript&qu ...
- 【C#学习笔记】string.Format对C#字符串格式化
文章转自:CSDN http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格 ...
- 2019Hdu多校第三场:1007 Find the answer(multiset 解法)
原题链接: Find the answer c++中,multiset是库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn)的时间内完成,而且他能时刻保证序列中的数 ...
- MySQL- 查询总结
查询总结 语法: select 查询字段 from 表 别名 连接类型inner|left|right join on 连接条件 where 筛选 group by 分组列表 having 筛选(二次 ...