nozomi和字符串
nozomi和字符串
http://www.nowcoder.com/questionTerminal/ebc0a750916541909e1e0377051b7e60
用尺取法维护一个区间改变k个字符的区间
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; string s; int n, k; int deal(char a, char a1)// change a to a1 { int L = 0, R = 0, change = 0, ans = 1; for (int i = 0; i < n; i++){ if (s[i] == a){ if (change < k){ change++; R++; } else{ while (L <= R && s[L] != a) L++; L++; R++; } } else R++; ans = max(ans, R - L ); } return ans; } int main(){ while (cin >> n >> k){ cin >> s; cout << max(deal('1', '0'), deal('0', '1')) << endl; } }