題目連接ios
題中無非三種狀況:c++
能夠先遍歷一遍,把全部VK找出來,計數,而後把VK換成「XX」,再進行第二次遍歷,找VV或者KK,若是找到,總數就加1,並退出循環。spa
/* * @Description: * @Author: 多多 * @Date: 2020-10-24 22:28:12 * @LastEditTime: 2020-10-24 22:32:09 * @LastEditors: 多多 */ #include <bits/stdc++.h> using namespace std; int main() { //freopen("P3741.in", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; int sum = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == 'V' && s[i + 1] == 'K') { sum++; s[i] = s[i + 1] = 'X'; } } for (int i = 0; i < n - 1; i++) { if ((s[i] == 'V' && s[i + 1] == 'V') || (s[i] == 'K' && s[i + 1] == 'K')) { sum++; break; } } cout << sum << endl; return 0; }