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 fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long int
#define ld long double
using namespace std;
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
int n, dp[100000][4][4][4][4];
string s;
long long go(int i, int pre1, int pre2, int pree1, int pree2){
if(i == 0){
long long ans = 0;
ans = max(ans, go(i + 1, (s[i] - 'A' + 1), pre1, pree1, pree2) + 1);
ans = max(ans, go(i + 1, pre1, pre2, (s[i] - 'A' + 1), pree1) + 1);
return ans;
}
if(i == n)
return 0;
if(dp[i][pre1][pre2][pree1][pree2] != -1)
return dp[i][pre1][pre2][pree1][pree2];
long long ans = 0;
if(i == 1){
if((s[i] - 'A' + 1) == pre1)
ans = max(ans, go(i + 1, (s[i] - 'A' + 1), pre1, pree1, pree2) + 1);
else if((s[i] - 'A' + 1) != pre1){
if(pre1 != 0)
ans = max(ans, go(i + 1, (s[i] - 'A' + 1), pre1, pree1, pree2) + 2);
else
ans = max(ans, go(i + 1, (s[i] - 'A' + 1), pre1, pree1, pree2) + 1);
}
if((s[i] - 'A' + 1) == pree1)
ans = max(ans, go(i + 1, pre1, pre2, (s[i] - 'A' + 1), pree1) + 1);
else if((s[i] - 'A' + 1) != pree1){
if(pree1 != 0)
ans = max(ans, go(i + 1, pre1, pre2, (s[i] - 'A' + 1), pree1) + 2);
else
ans = max(ans, go(i + 1, pre1, pre2, (s[i] - 'A' + 1), pree1) + 1);
}
return dp[i][pre1][pre2][pree1][pree2] = ans;
}
if(pre1 == 0 and pre2 == 0)
ans = max(ans, go(i + 1, s[i] - 'A' + 1, pre1, pree1, pree2) + 1);
else if(pre2 == 0){
if(s[i] - 'A' + 1 == pre1)
ans = max(ans, go(i + 1, s[i] - 'A' + 1, pre1, pree1, pree2) + 1);
else
ans = max(ans, go(i + 1, s[i] - 'A' + 1, pre1, pree1, pree2) + 2);
}
else{
if(s[i] - 'A' + 1 == pre1 and s[i] - 'A' + 1 == pre2)
ans = max(ans, go(i + 1, s[i] - 'A' + 1, pre1, pree1, pree2) + 1);
else if(s[i] - 'A' + 1 == pre1 or s[i] - 'A' + 1 == pre2)
ans = max(ans, go(i + 1, s[i] - 'A' + 1, pre1, pree1, pree2) + 2);
else{
if(pre1 != pre2)
ans = max(ans, go(i + 1, s[i] - 'A' + 1, pre1, pree1, pree2) + 3);
else
ans = max(ans, go(i + 1, s[i] - 'A' + 1, pre1, pree1, pree2) + 2);
}
}
if(pree1 == 0 and pree2 == 0)
ans = max(ans, go(i + 1, pre1, pre2, s[i] - 'A' + 1, pree1) + 1);
else if(pree2 == 0){
if(s[i] - 'A' + 1 == pree1)
ans = max(ans, go(i + 1, pre1, pre2, s[i] - 'A' + 1, pree1) + 1);
else
ans = max(ans, go(i + 1, pre1, pre2, s[i] - 'A' + 1, pree1) + 2);
}
else{
if(s[i] - 'A' + 1 == pree1 and pree1 == pree2)
ans = max(ans, go(i + 1, pre1, pre2, s[i] - 'A' + 1, pree1) + 1);
else if(s[i] - 'A' + 1 == pree1 or s[i] - 'A' + 1 == pree2)
ans = max(ans, go(i + 1, pre1, pre2, s[i] - 'A' + 1, pree1) + 2);
else{
if(pree1 != pree2)
ans = max(ans, go(i + 1, pre1, pre2, s[i] - 'A' + 1, pree1) + 3);
else
ans = max(ans, go(i + 1, pre1, pre2, s[i] - 'A' + 1, pree1) + 2);
}
}
return dp[i][pre1][pre2][pree1][pree2] = ans;
}
void solve(){
memset(dp, -1, sizeof dp);
cin >> n >> s;
for(int i = 0; i < s.size(); ++i){
if(s[i] == 'M') s[i] = 'A';
else if(s[i] == 'F') s[i] = 'C';
}
cout << go(0, 0, 0, 0, 0) << '\n';
}
int main(){
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
return 0;
}
Compilation message (stderr)
miners.cpp: In function 'void solve()':
miners.cpp:88:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int i = 0; i < s.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... |