This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 100001,INF=1e12;
int n;
string s;
int dp[N][4][4][4][4];
int check(int a,int b,int c){
set<int>st;
if(a!=0) st.insert(a);
if(b!=0) st.insert(b);
if(c!=0) st.insert(c);
return st.size();
}
int solve(int i,int preva1,int prevb1,int preva2,int prevb2)
{
//cout<<i<<" "<<preva1<<" "<<preva2<<" "<<prevb1<<" "<<prevb2<<endl;
if(i==n) return 0;
int ans=0;
if(dp[i][preva1][prevb1][preva2][prevb2]!=-1) return dp[i][preva1][prevb1][preva2][prevb2];
int x; if(s[i]=='M') x=1; else if(s[i]=='B') x=2; else x=3;
ans=solve(i+1,preva1,prevb1,prevb2,x)+check(preva2,prevb2,x);
ans=max(ans,solve(i+1,prevb1,x,preva2,prevb2)+check(preva1,prevb1,x));
return dp[i][preva1][prevb1][preva2][prevb2]=ans;
}
int32_t main()
{
//freopen("abc.in", "r", stdin);
cin >> n ;
cin>>s;
memset(dp,-1,sizeof dp);
cout<<solve(0,0,0,0,0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |