#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long int ll;
const int N=1e5+7;
string s;
int n;
int c(int a,int b,int c){
int sol = 0;
if (a == 1 || b == 1 || c == 1) sol++;
if (a == 2 || b == 2 || c == 2) sol++;
if (a == 3 || b == 3 || c == 3) sol++;
return sol;
}
int a[N];
int dp[N][4][4][4][4];
int solve(int idx,int prea1,int prea2,int preb1,int preb2){
if(idx==n) {
return 0;
}
int &ans=dp[idx][prea1][prea2][preb1][preb2];
if(ans!=-1) return ans;
int choice1=solve(idx+1,a[idx],prea1,preb1,preb2)+c(a[idx],prea1,prea2);
int choice2=solve(idx+1,prea1,prea2,a[idx],preb1)+c(a[idx],preb1,preb2);
return ans=max(choice1,choice2);
}
int main()
{
ios_base:: sync_with_stdio(false); cin.tie(0);
cin>>n;
cin>>s;
for(int i=0; i<n; i++) {
if(s[i]=='B') {
a[i]=3;
}
else if(s[i]=='M') {
a[i]=1;
}
else{
a[i]=2;
}
}
memset(dp,-1,sizeof dp);
int ans=solve(0,0,0,0,0);
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
100604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
100472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
100564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
100548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
100472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
100552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
100572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
101028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
101368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
102648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
209 ms |
107000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
288 ms |
109036 KB |
Output is correct |