Codeforces 877(442 Div.2) B. Nikita and string
来源:互联网 发布:java锁有几种 编辑:程序博客网 时间:2024/05/21 03:54
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Print a single integer — the maximum possible size of beautiful string Nikita can get.
abba
4
bab
2
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
题意:
一个只有'a','b'组成的字符串可以分割为3个字符串且不能打乱之前的顺序,要保证第1个和第3个字符串全为'a',第二个全为'b'(这3个字符串可为空,如"a","b",""是满足条件的)。现在你可删除若干个字符,在满足条件下尽可能使得字符串长度最大,输出它的长度。
思路:
我们记dp[i][0]表示最长aaa......形式的子序列,dp[i][1]表示最长aaa......bbb......形式的子序列,dp[i][2]表示最长aaa......bbb......aaa......形式的子序列,最后三种情况取最大即可。
那么状态转移方程为:
if(s[i]=='a'){ dp[i][0]=dp[i-1][0]+1; dp[i][1]=max(dp[i-1][1],dp[i][0]); dp[i][2]=max(dp[i-1][2]+1,dp[i-1][1]+1);}else{ dp[i][0]=dp[i-1][0]; dp[i][1]=max(dp[i-1][1]+1,dp[i][0]); dp[i][2]=dp[i-1][2];}示例程序:
#include <bits/stdc++.h>#define LDQ 1000000007#define QAQ 0x3f3f3f3fusing namespace std;int dp[5001][3];int main(){ int len,i; char s[5001]; scanf("%s",s); len=strlen(s); for(i=1;len>=i;i++) { if(s[i-1]=='a') { dp[i][0]=dp[i-1][0]+1; dp[i][1]=max(dp[i-1][1],dp[i][0]); dp[i][2]=max(dp[i-1][2]+1,dp[i-1][1]+1); } else { dp[i][0]=dp[i-1][0]; dp[i][1]=max(dp[i-1][1]+1,dp[i][0]); dp[i][2]=dp[i-1][2]; } } printf("%d",max(dp[len][0],max(dp[len][1],dp[len][2]))); return 0;}
- Codeforces 877(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 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(线性DP)
- Codeforces 877 B Nikita and string
- Codeforces 877 B. Nikita and string (技巧)
- B. Nikita and string
- codeforces 877 problem 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
- CF#877 B. Nikita and string(思维,暴力)
- Nikita and string
- Nikita and string
- Codeforces Round #297 (Div. 2) B. Pasha and String
- Centos7安装部署Redis3.2.9 (普通用户)
- 题目84:阶乘的0
- python import
- 关于对象创建及初始化的面试向理解
- webrtc 接收packt包后处理
- Codeforces 877(442 Div.2) B. Nikita and string
- angularJs购物车
- iOS计算一段代码执行时间
- 【数字视音频处理】【Qtcreator】音乐DIY 设计
- PHP--SOCKET功能详解 及SOCKET编程
- bootstrap 表单验证formValidation的remote填坑
- sqlserver如何从带分隔符的字符串创建多个列
- 使用ExpandListView二级列表实现购物车
- Java程序初始化的顺序