Description
One day Nikita found the string containing letters "a" and "B" only.
Nikita thinks that string are 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 o Rder. What is the maximum length of the the string he can get?
Input
The contains a non-empty string of length not greater than 5 containing only lowercase Chinese letters "a" and "B".
Output
Print a single integer-the maximum possible the size of beautiful string Nikita can get.
examples Input
Abba
examples Output
4
the
We want to find a subsequence in the original string, which is full of a, the left half of the subsequence and the right half, the middle part is all B (each part can be empty), and ask the length of the sequence to be the longest.
train of Thought
We consider recording the number of a,b a,b that each prefix appears, where Prea[i] prea[i] represents the number of a in the first I element, Preb[i] Preb[i] represents the number of b b in the first I element.
Then constructs the double pointer i,j i,j to do the dividing line, satisfies the 0≤i≤j≤n 0≤i≤j≤n, then obtains the sequence the length is n− (preb[i]+ (preb[n]−preb[j)) + (prea[j]−prea[i))) n (preb[i]+) (Preb [N]-preb[j]) + (Prea[j]-prea[i]).
AC Code
#include <bits/stdc++.h> #define IO Ios::sync_with_stdio (false); \ cin.tie (0); \ cout.
Tie (0);
#define INF 0x7f7f7f7f using namespace std;
typedef __int64 LL;
const int MAXN = 1E5+10;
Char STR[MAXN];
int PREA[MAXN],PREB[MAXN];
void Solve () {int len = strlen (str+1);
for (int i=1; i<=len; i++) {prea[i] = prea[i-1];
Preb[i] = preb[i-1];
if (str[i]== ' a ') prea[i]++;
else preb[i]++;
int ans = 0; for (int i=0; i<=len; i++) {for (int j=i; j<=len; J + +) {int now = preb[i]+preb[len]-
Preb[j]+prea[j]-prea[i];
ans = max (ans,len-now);
} printf ("%d\n", ans);
int main () {gets (str+1);
Solve ();
return 0; }