Codeforces Round #442(Div.2) B.Nikita and string(线性DP)
来源:互联网 发布:千牛首页的店铺数据 编辑:程序博客网 时间:2024/06/03 22:57
题目链接:Nikita and string
题意:给出一个长度<=5000的a-b串,允许移除串的某些字符,其他字符的相对位置不变,使得串能被切割成三个子串S1,S2,S3,满足S1、S3只含字符a或者为空,S2只含字符b或者为空。问移除字符过后的串的长度最大为几?
题解:我采用DP做法,目标求长度最长,在满足要求的条件下。设置状态:开大小为dp[5010][3]的数组,dp[i][0]表示第i位前没有出现过b,第i位是a;dp[i][1]表示第i位前出现过b,第i位是a;dp[i][2]表示第i位是b。
所有可能情况的状态转移:
1.如果第i位是a的情况,组合①…a(j) a(i) ②…ab(j) a(i) ③…ba(j) a(i) [PS:a(i)标识第i位是a] 都是被允许的。
情况①:dp[i][0] = max(dp[i][0],dp[j][0]+1);
情况②:dp[i][1] = max(dp[i][1],dp[j][2]+1);
情况③:dp[i][1] = max(dp[i][1],dp[j][1]+1);
2.如果第i位是b的情况,组合①….a(j) b(i) ②…b(j) b(i)
注意..b..a(j) b(i)的情况是非法的,bab是要求删除部分字符的情况,而我们DP是找合法情况的最大长度值(*)
情况①:dp[i][2] = max(dp[i][1],dp[j][0]+1);
情况②:dp[i][2] = max(dp[i][1],dp[j][2]+1);
n方循环找最长,时间复杂度O(n^2)
#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <queue>#include <string>#include <cstring>#include <map>#include <stack>#include <set>#define Max(a,b) a>b?a:b#define Min(a,b) a>b?b:a#define mem(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long ll;int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};const double eps = 1e-6;const double Pi = acos(-1.0);const int INF=0x3f3f3f3f;const int maxn = 5010;char str[maxn];int dp[maxn][3],n;int main(){ memset(dp,0,sizeof(dp)); scanf("%s",str); n = strlen(str); if(str[0] == 'a') dp[0][0] = dp[0][1] = 1; else dp[0][2] = 1; for(int i = 1; i < n; i++){ if(str[i] == 'a'){ for(int j = 0; j < i; j++){ dp[i][1] = max(dp[i][1],dp[j][1]+1); dp[i][1] = max(dp[i][1],dp[j][2]+1); dp[i][0] = max(dp[i][0],dp[j][0]+1); } }else{ for(int j = 0; j < i; j++){ dp[i][2] = max(dp[i][2],dp[j][0]+1); dp[i][2] = max(dp[i][2],dp[j][2]+1); } } } int ans = 0; for(int i = 0; i < n; i++){ ans = max(max(ans,dp[i][2]),max(dp[i][0],dp[i][1])); } printf("%d\n",ans); return 0;}
阅读全文
0 0
- 【Codeforces Round #442 (Div. 2)】 B. Nikita and string (线性状态dp)
- Codeforces Round #442(Div.2) B.Nikita and string(线性DP)
- Codeforces Round #442 (Div. 2) B. Nikita and string 题解
- Codeforces Round #442 (Div. 2) B. Nikita and string
- Codeforces Round #442 (Div. 2) B. Nikita and string
- Codeforces Round #442 (Div. 2) B. Nikita and string (记忆化搜索)
- Codeforces 877(442 Div.2) B. Nikita and string
- 南华新生训练2017- (思维题)codeforces Round 442 B Nikita and string
- Codeforces Round #430 (Div. 2) E. Nikita and game
- Codeforces Round #430 (Div. 2) E. Nikita and game
- Codeforces 877 B Nikita and string
- B. Nikita and string
- Codeforces Round #297 (Div. 2) B. Pasha and String
- Codeforces Round #297 (Div. 2)B Pasha and String
- Codeforces Round #297 (Div. 2) B - Pasha and String
- Codeforces 877 B. Nikita and string (技巧)
- Codeforces Round #240 (Div. 1)---B.Mashmokh and ACM(dp)
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(倍增LCA+树形DP)
- FPGA之有限状态机
- HandlerThread 源码解析
- 高级命令行文本处理cut,sed,awk
- Parted分区
- JS插件(1)--- autocomplete 异步加载下拉数据
- Codeforces Round #442(Div.2) B.Nikita and string(线性DP)
- 深度学习与神经网络-吴恩达(Part2Week3)-超参数调试、Batch正则化和程序框架
- pytorch学习笔记(二) 其他机制
- 欧拉函数 codeforces 776E
- 第八周项目3 对矩阵压缩存储的实现与应用(2)
- TensorFlow-3-Basic Models
- FCC算法:四、Find the Longest Word in a String -- 找出最长单词
- Python----运算符, 切片
- Kafka 实现的几个技术细节讨论