codeforces #442 B
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.
自己程式碼不忍直視 貼一個巨巨的 程式碼好強
#include<stdio.h>
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
std::ios::sync_with_stdio(false);
string s;
cin>>s;
int n=s.length(),i;
int a=0,aba=0,ab=0;
for(i=0;i<n;i++)
{
if(s[i]=='a')
aba++,a++; //記錄總長度最長 和 a 的長度
if(s[i]=='b')
ab++; //記錄 aaaa這樣的最長 還是 aaaaabbbb這種型別的 最長的長度
aba=max(aba,ab);
ab=max(ab,a);
}
cout<<aba;
return 0;
}
相關文章
- codeforces_B. Barnicle
- CodeForces - 1363B
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)B
- Codeforces 194B.Square
- Codeforces Round #399 (A,B,C)
- codeforces 498B. Name That Tune
- CodeForces 1132B Discounts
- Codeforces 264B. Good SequencesGo
- CodeForces 1417B Two Arrays
- Codeforces Round #448 (Div. 2)B
- Codeforces Round #450 (Div. 2) B
- codeforces 101 B. Buses
- Codeforces Round 840題解(A、B、C)
- codeforces round 961題解(A、B、C)
- Codeforces 549B. Looksery Party[構造]
- Codeforces 687B. Remainders Game[剩餘]REMAIGAM
- CodeForces - 282B Painting EggsAI
- CodeForces703B Mishka and trip 數學
- Codeforces 294B Shaass and Bookshelf:dp
- 【dp】CodeForces - 623B Array GCDGC
- Codeforces Testing Round #10 B. Balancer
- Codeforces Round #251 (Div. 2) A/B/D
- codeforces 260 div2 A,B,C
- Codeforces 582 B Once Again... (LIS)AI
- Codeforces Round 949題解(A、B、C、D)
- CF741B:https://codeforces.com/problemset/problem/741/BHTTP
- codeforces round #234B(DIV2) B Inna and New Matrix of Candies
- HDD is Outdated Technology Codeforces 612B
- 【Codeforces Round #499 (Div. 1) B】Rocket
- Codeforces 500B. New Year Permutation[連通性]
- Codeforces 854B Maxim Buys an Apartment:貪心
- Codeforces Round #362 (Div. 2) B 模擬
- Codeforces Round #336 (Div. 2) B 暴力
- Codeforces Round #325 (Div. 2) B 遞推
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #245 (Div. 2) B - Balls GameGAM
- codeforces 651B Beautiful Paintings (想法)AI
- Codeforces Round 934 2D/1B