codeforces 801A Vicious Keyboard

题目链接:http://codeforces.com/contest/801/problem/A

题意:给你一个由字符V和K组成的字符串,问你最多改变一个字符,求字符串中最多出现多少个"VK";

分析:我是直接扫一遍求出字符串目前有多少"VK",并把它们变成其他字符,例如"00",然后再扫一遍找有没有两个连续的V或者K,有的话输出+1;

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main() {
 4     ios_base::sync_with_stdio(0);
 5 //freopen("out1.txt","w",stdout);
 6     int n;
 7     char c[105];
 8     cin>>c;
 9     int d=strlen(c);
10     if(d==1) {
11         cout<<0<<endl;
12     }
13     else {
14         int ans=0;
15         int p=0;
16         for(int i=0;i<d-1;i++){
17             if(c[i]=='V'&&c[i+1]=='K'){
18                 c[i]='0';
19                 c[i+1]='0';
20                 i++;
21                 ans++;
22             }
23         }
24         for(int i=0;i<d-1;i++){
25             if(c[i]=='V'&&c[i+1]=='V'){
26                 p=1;
27                 break;
28             }
29             else if(c[i]=='K'&&c[i+1]=='K'){
30                 p=1;
31                 break;
32             }
33         }
34         if(p==1){
35             cout<<ans+1<<endl;
36         }
37         else cout<<ans<<endl;
38     }
39     return 0;
40 }
View Code
原文地址:https://www.cnblogs.com/ls961006/p/6921339.html