题解一(暴力枚举):
因为n只有350,所以可以考虑暴力枚举每一个断点,时间复杂度n2。
需要注意最后的答案不能大于n!
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PIL pair<int, ll>
#define endl '\n'
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define IOS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define debug(x) cerr << #x << " = " << x << endl
#define debug2(x, y) cerr << #x << " = " << x << " and " << #y << " = " << y << endl
#define Pi acos(-1)
using namespace std;
const int N = 5e5 + 100;
string a;
int f(int x)
{
int res = 0;
string p = "";
for(int i = x; i < a.size(); ++ i) p += a[i];
for(int i = 0; i < x; ++ i) p += a[i];
//模拟剪短过程,断口的端口就是p的左右端点
//向后找最多多少个
int k = 0;
while(p[k] == 'w') k ++, res ++;
for(int i = k; i < p.size(); ++ i)
{
if(p[i] != p[k] && p[i] != 'w') break;
res ++;
}
//向前找最多多少个
k = p.size() - 1;
while(p[k] == 'w') k --, res ++;
for(int i = k; i >= 0; -- i)
{
if(p[i] != p[k] && p[i] != 'w') break;
res ++;
}
return res;
}
void solve()
{
int n;
int MAX = -1;
cin >> n;
cin >> a;
for(int i = 0; i < a.size(); ++ i) //枚举每一个断点
MAX = max(MAX, f(i));
cout << min(n, MAX) << endl;
}
int main()
{
IOS;
solve();
return 0;
}
题解二(dp):