Codeforce 803E. Roma and Poker(dp)
来源:互联网 发布:linux init 0 编辑:程序博客网 时间:2024/06/07 23:29
题意:给你一个n长度的字符串,‘W’和‘L’个数的差值在中途要小于k,且在最后的时候差值正好为k,‘D’表示平局,‘?’表示不确定。问是否存在这样的字符串,存在就输出,不存在就NO。
解法:首先可以用一个dp在确定在这过程能否有这样的情况产生。
用dp[i][j]表示前i个字符‘W’和‘L’相差j个是否存在。那么
如果s[i]=‘W’ dp[i][j]=dp[i-1][j-1];
如果s[i]=‘L’ dp[i][j]=dp[i-1][j+1];
如果s[i]=‘D’ dp[i][j]=dp[i-1][j];
如果s[i]=‘?’ dp[i][j]=max(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1]);
这样确定了之后只要从后往前遍历找到一个符合条件的就可以了,这里注意j可以小于0,我在这里把j加上了个1001,这样j=1001就表示相差为0的时候。不多说了,下面上代码
#include<iostream>#include<cstring>#include<math.h>#include<cstdio>#include<algorithm>#define N 6005#define INF 0x3f3f3f3f#include<map>#include<string>#include<queue>#include<vector>#include<set>#define mod 1000000007double pi=acos(-1.0);typedef long long ll;using namespace std;int dp[1005][2005];int n,k;string s;int main(){ cin>>n>>k; cin>>s; memset(dp,0,sizeof(dp)); dp[0][1001]=1; for(int i=1;i<=s.size();i++) for(int j=(-1)*k;j<=k;j++) { if(i!=n&&(j==(-1)*k||j==k)) continue; if(s[i-1]=='W') dp[i][j+1001]=dp[i-1][j+1001-1]; else if(s[i-1]=='L') dp[i][j+1001]=dp[i-1][j+1001+1]; else if(s[i-1]=='D') dp[i][j+1001]=dp[i-1][j+1001]; else { int da=0; da=max(dp[i-1][j+1001],da); da=max(da,dp[i-1][j+1001+1]); da=max(da,dp[i-1][j+1001-1]); dp[i][j+1001]=da; } } if(dp[n][k+1001]==0&&dp[n][1001-k]==0) { printf("NO\n"); return 0; } //printf("1\n"); if(dp[n][k+1001]>0) { int cha=k+1001,flag=0; for(int i=n-1;i>=0;i--) { if(s[i]!='?') { if(s[i]=='W') cha--; else if(s[i]=='L') cha++; if(cha==k+1001||cha==1001-k) { flag=1; break; } } else { if(cha!=1001+k&&cha!=1001-k&&dp[i][cha]==1) s[i]='D'; else if(cha+1<1001+k&&dp[i][cha+1]==1) { s[i]='L'; cha++; } else if(cha-1>1001-k&&dp[i][cha-1]==1) { s[i]='W'; cha--; } else { flag=1; break; } } } if(!flag) { cout<<s<<endl; return 0; } } if(dp[n][1001-k]>0) { int cha=1001-k,flag=0; for(int i=s.size()-1;i>=0;i--) { if(s[i]!='?') { if(s[i]=='W') cha--; else if(s[i]=='L') cha++; if(cha==k+1001||cha==1001-k) { flag=1; break; } } else { if(cha!=1001+k&&cha!=1001-k&&dp[i][cha]==1) s[i]='D'; else if(cha+1<1001+k&&dp[i][cha+1]==1) { s[i]='L'; cha++; } else if(cha-1>1001-k&&dp[i][cha-1]==1) { s[i]='W'; cha--; } else { flag=1; break; } } } if(!flag) { cout<<s<<endl; return 0; } } printf("NO\n"); return 0;}
0 0
- Codeforce 803E. Roma and Poker(dp)
- codeforces 803E Roma and Poker (dp)
- Codeforces 803E Roma and Poker【Dp+记录路径】
- Codeforces803E Roma and Poker
- cf Educational Codeforces Round 20 E. Roma and Poker
- Codeforces #803E: Roma and Pokers 题解
- Codeforce - 262 - B. Roma and Changing Signs
- codeforce 66E - Petya and Post(dp+减少冗余)
- Codeforce 490E(树形dp)
- CodeForce #318 div 2 C Bear and Poker
- codeforce #320E Weakness and Poorness (三分)
- codeforce 342E Xenia and Tree(分块 + LCA)
- CodeForce 710E - Generate a String(dp)
- Codeforce 489E(dp+01分数规划)
- Codeforce#394E. Dasha and Puzzle(dfs)
- E. Ostap and Tree(树形dp)
- codeforce Ilya and Roads (区间dp)
- Codeforce 467C. George and Job(DP)
- android自定义绘图
- 小马哥---高仿山寨三星S8出世 谨防购买到假货!! 高仿三星S8 s305刷机拆机主板图示
- C++11 Scoped Enumeration
- 练习题代码
- 关于转换构造函数与转换函数_boolan_第二门课_第一周
- Codeforce 803E. Roma and Poker(dp)
- 运动目标跟踪(一)--搜索算法预测模型之KF,EKF,UKF
- 数位dp、
- bzoj 4259 残缺的字符串
- tomcat出现An internal error occurred during: "Launching sushe on MyEclipse Tomcat ".
- 写一个【栈】模板,并实现“任意进制转换器”
- burp suit漏洞扫描工具
- Cookie VS. Session
- 调用存储过程和返回结果集