#include <bits/stdc++.h>
using namespace std;
int arr[100001];
int dp[100001][4][4][4][4];
int n;
int rec(int pos, int last_l, int plast_l, int last_r, int plast_r)
{
if (pos == n)
{
return 0;
}
if (dp[pos][last_l][plast_l][last_r][plast_r] != -1)
{
return dp[pos][last_l][plast_l][last_r][plast_r];
}
int inc = 1;
if (last_l != arr[pos] and last_l != 0)
{
inc++;
}
if (plast_l != arr[pos] and plast_l != last_l and plast_l != 0)
{
inc++;
}
dp[pos][last_l][plast_l][last_r][plast_r] = rec(pos+1, arr[pos], last_l, last_r, plast_r) + inc;
inc = 1;
if (last_r != arr[pos] and last_r != 0)
{
inc++;
}
if (plast_r != arr[pos] and plast_r != last_r and plast_r != 0)
{
inc++;
}
dp[pos][last_l][plast_l][last_r][plast_r] = max (dp[pos][last_l][plast_l][last_r][plast_r], rec(pos+1, last_l, plast_l, arr[pos], last_r) + inc);
return dp[pos][last_l][plast_l][last_r][plast_r];
}
int main()
{
memset(dp, -1, sizeof(dp));
cin>>n;
for (int i=0; i<n; i++)
{
char c;
cin>>c;
if (c=='M')
{
arr[i] = 1;
}
else if (c=='B')
{
arr[i] = 2;
}
else
{
arr[i] = 3;
}
}
cout<<rec(0, 0, 0, 0, 0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
100468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
100460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
100400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
100392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
100436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
100412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
100468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
100924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
101528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
103300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
108980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
224 ms |
111824 KB |
Output is correct |