|
发表于 2023-2-26 17:53:18
|
显示全部楼层
我没有测试,不懂对不对:- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- int n, k;
- string s;
- cin >> n >> k >> s;
- int count = 0;
- for (int i = 1; i < n; i++) {
- if (s[i] == s[i - 1] && s[i] == 'Y') {
- count++;
- }
- }
- int max_count = count;
- for (int i = 0; i < k; i++) {
- char c = (s[i] == 'X') ? 'Y' : 'X';
- int temp_count = count;
- if (i > 0 && s[i - 1] == s[i]) {
- temp_count--;
- }
- if (i < n - 1 && s[i + 1] == s[i]) {
- temp_count--;
- }
- if (i > 0 && s[i - 1] == c) {
- temp_count++;
- }
- if (i < n - 1 && s[i + 1] == c) {
- temp_count++;
- }
- max_count = max(max_count, temp_count);
- }
- cout << max_count << endl;
- return 0;
- }
复制代码 |
|