https://www.nowcoder.com/acm/contest/134/I
题解
1.dp[i][0]表示初始的连续女生数
2.dp[i][1]表示经过互换后的连续女生数
当没有操作时dp[i][0]==dp[i][1],操作后,若再次出现男生则dp[i][1]被初始化为dp[i][1]+1,
也就是枚举了第二个操作的位置
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;
const int MAXN = 1e6 + 10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const double eps = 1e-8;
int n, m, k;
string str;
vii dp;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
cin >> n;
dp.resize(n, vi(2));
cin >> str;
cerr << str << endl;
int res = 0, cnt = 0;
cnt = dp[0][0] = !(str[0] - '0');
dp[0][1] = 1;
for(int i = 1; i < n; i++){
if(!(str[i] - '0')){
cnt++;
dp[i][0] = dp[i - 1][0] + 1;
dp[i][1] = dp[i - 1][1] + 1;
}
else{
dp[i][0] = 0;
dp[i][1] = dp[i - 1][0] + 1;
}
res = max(res, max(dp[i][0], dp[i][1]));
}
cout << min(res, cnt) << endl;
cerr << "execute time : " << (double)clock() / CLOCKS_PER_SEC << endl;
return 0;
}