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>
using namespace std;
#define cin(vec) for(auto& i : vec) cin >> i
#define cout(vec) for(auto& i : vec) cout << i << " "; cout << "\n";
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define loop(i,a,b) for (int i = a; i < b; i++)
#define F first
#define S second
#define pb(n) push_back(n)
#define pf(n) push_front(n)
#define dci(d) fixed<<setprecision(d)
#define sp ' '
#define el '\n'
#define all(v) v.begin(),v.end()
#define int long long
int dx[8]= {0,0,1,-1,-1,1,1,-1};
int dy[8]= {-1,1,0,0,-1,1,-1,1};
int const N=1e5+1,M=1e3+1,Mod=1e9+7;
string s;
int n;
map<pair<int,pair<deque<char>,deque<char>>>,int>dp;
map<pair<int,pair<deque<char>,deque<char>>>,bool>vis;
int rec(int idx,deque<char>msk1,deque<char>msk2){
if(idx==n)return 0;
if(vis[make_pair(idx,make_pair(msk1,msk2))]){
return dp[make_pair(idx,make_pair(msk1,msk2))];
}
vis[make_pair(idx,make_pair(msk1,msk2))]=1;
deque<char>before=msk1,before2=msk2;
if(msk1.size()==3)
msk1.pop_front();
msk1.push_back(s[idx]);
int m=0,b=0,f=0;
for(int i=0;i<msk1.size();i++){
m+=msk1[i]=='M';
b+=msk1[i]=='B';
f+=msk1[i]=='F';
}
int c1=rec(idx+1,msk1,msk2)+(m>0)+(b>0)+(f>0);
if(msk2.size()==3){
msk2.pop_front();
}
msk2.push_back(s[idx]);
m=0,b=0,f=0;
for(int i=0;i<msk2.size();i++){
m+=msk2[i]=='M';
b+=msk2[i]=='B';
f+=msk2[i]=='F';
}
int c2=rec(idx+1,before,msk2)+(m>0)+(b>0)+(f>0);
return dp[make_pair(idx,make_pair(before,before2))]=max(c1,c2);
}
void testcase(int h){
cin>>n>>s;
deque<char>q;
cout<<rec(0,q,q);
}
int32_t main()
{
fast
testcase(1);
// int tc;cin>>tc;for(int i=1;i<=tc;i++)testcase(i);
return 0;
}
Compilation message (stderr)
miners.cpp: In function 'long long int rec(long long int, std::deque<char>, std::deque<char>)':
miners.cpp:34:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i=0;i<msk1.size();i++){
| ~^~~~~~~~~~~~
miners.cpp:45:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0;i<msk2.size();i++){
| ~^~~~~~~~~~~~
# | 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... |