Codeforces 801A 暴力 string

传送门:题目

题意:

给一个字符串,里面只有VK字母,计算所有出现VK的次数,你有且只有一次转换的机会,可以把V转成K,或者把K转成V,你可以不使用这次机会。

题解:

题目很水,先统计一次VK的个数,然后再统计一下VV或者KK的个数,如果有,结果+1就好,当时写了20多行,赛后看题解,发现题解Perl只用了一行,C++算头文件只用了5行,用了string.find()方法,很方便。

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define debug(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
using namespace std;

int main(void){
    int ans=0;
    string str;
    cin>>str;
    while(str.find("VK")!=string::npos){
        int temp=str.find("VK");
        str[temp]='.';
        str[temp+1]='.';
        ans++;
    }    
    if(str.find("VV")!=string::npos||str.find("KK")!=string::npos)
    //这里不要写成-1,因为npos是size_t的最大值,64位操作系统和32位操作系统的size_t是不同的。
        ans++;
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shadandeajian/article/details/81808475