题解:P3741 小果的键盘
题解:P3741 小果的键盘
题目传送门
本题只需要对字符串进行循环,当发现VK时将计数变量自增并将VK改为.,最后再遍历一次,当发现VV或 KK 时说明改变一次可以出现VK,将计数变量加一并输出即可。
AC code
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;//字符串长度
string s;//含有VK的字符串
cin>>n>>s;
int c=0;//计数变量
for(int i=0;i<n-1;i++){//寻找VK
if(s[i]=='V'&&s[i+1]=='K'){//发现VK
c++;//计数变量自增
s[i]=s[i+1]='.';//将VK改为.
}
}
for(int i=0;i<n-1;i++){//寻找VV或KK
if(s[i]==s[i+1]&&s[i]!='.'){//如果发现VV或KK
cout<<c+1;//输出计数变量加一
return 0;//直接退出
}
}
cout<<c;//如果无法改变,输出计数变量,即VK的个数
return 0;//完结撒花
}