이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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][3][3][3][3][3][3];
int solve(int i,int M1,int B1,int F1,int M2,int B2,int F2)
{
//cout<<i<<" "<<M1<<" "<<B1<<" "<<F1<<" "<<M2<<" "<<B2<<" "<<F2<<endl;
if(i==n) return 0;
int ans=0;
if(M1!=-1&&B1!=-1&&F1!=-1&&M2!=-1&&B2!=-1&&F2!=-1&&dp[i][M1][B1][F1][M2][B2][F2]!=-1) return dp[i][M1][B1][F1][M2][B2][F2];
if(s[i]=='M'){
int m1=M1,b1=B1,f1=F1;
m1=0,b1++,f1++;
if(B1==-1 || b1>2) b1=-1;
if(F1==-1 || f1>2) f1=-1;
ans = solve(i+1,m1,b1,f1,M2,B2,F2)+((m1>=0)+(b1>=0)+(f1>=0));
m1=M2,b1=B2,f1=F2;
m1=0,b1++,f1++;
if(B2==-1 || b1>2) b1=-1;
if(F2==-1 || f1>2) f1=-1;
ans=max(ans,solve(i+1,M1,B1,F1,m1,b1,f1)+((m1>=0)+(b1>=0)+(f1>=0)));
}
else if(s[i]=='B'){
int m1=M1,b1=B1,f1=F1;
m1++,b1=0,f1++;
if(M1==-1 || m1>2) m1=-1;
if(F1==-1 || f1>2) f1=-1;
ans = solve(i+1,m1,b1,f1,M2,B2,F2)+((m1>=0)+(b1>=0)+(f1>=0));
m1=M2,b1=B2,f1=F2;
m1++,b1=0,f1++;
if(M2==-1 || m1>2) m1=-1;
if(F2==-1 || f1>2) f1=-1;
ans=max(ans,solve(i+1,M1,B1,F1,m1,b1,f1)+((m1>=0)+(b1>=0)+(f1>=0)));
}
else if(s[i]=='F'){
int m1=M1,b1=B1,f1=F1;
m1++,b1++,f1=0;
if(M1==-1 || m1>2) m1=-1;
if(B1==-1 || b1>2) b1=-1;
ans = solve(i+1,m1,b1,f1,M2,B2,F2)+((m1>=0)+(b1>=0)+(f1>=0));
m1=M2,b1=B2,f1=F2;
m1++,b1++,f1=0;
if(M2==-1 || m1>2) m1=-1;
if(B2==-1 || b1>2) b1=-1;
ans=max(ans,solve(i+1,M1,B1,F1,m1,b1,f1)+((m1>=0)+(b1>=0)+(f1>=0)));
}
if(M1!=-1&&B1!=-1&&F1!=-1&&M2!=-1&&B2!=-1&&F2!=-1)
return dp[i][M1][B1][F1][M2][B2][F2]=ans;
else return ans;
}
int32_t main()
{
//freopen("abc.in", "r", stdin);
cin >> n ;
cin>>s;
memset(dp,-1,sizeof dp);
cout<<solve(0,-1,-1,-1,-1,-1,-1);
}
# | 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... |