zoukankan      html  css  js  c++  java
  • Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)A. Vicious Keyboard

    A. Vicious Keyboard
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Tonio has a keyboard with only two letters, "V" and "K".

    One day, he has typed out a string s with only these two letters. He really likes it when the string "VK" appears, so he wishes to change at most one letter in the string (or do no changes) to maximize the number of occurrences of that string. Compute the maximum number of times "VK" can appear as a substring (i. e. a letter "K" right after a letter "V") in the resulting string.

    Input

    The first line will contain a string s consisting only of uppercase English letters "V" and "K" with length not less than 1 and not greater than 100.

    Output

    Output a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.

    Examples
    input
    VK
    output
    1
    input
    VV
    output
    1
    input
    V
    output
    0
    input
    VKKKKKKKKKVVVVVVVVVK
    output
    3
    input
    KVKV
    output
    1
    Note

    For the first case, we do not change any letters. "VK" appears once, which is the maximum number of times it could appear.

    For the second case, we can change the second character from a "V" to a "K". This will give us the string "VK". This has one occurrence of the string "VK" as a substring.

    For the fourth case, we can change the fourth character from a "K" to a "V". This will give us the string "VKKVKKKKKKVVVVVVVVVK". This has three occurrences of the string "VK" as a substring. We can check no other moves can give us strictly more occurrences.

    题目大意:一个字符串只由V和K组成,你可以改变一个字符串最多一个字母,问你最多能得到几个VK

    题目解析:单个枚举变的字母,计算VK的个数,记录最大值

    #include <bits/stdc++.h>
    
    using namespace std;
    const int MAXN = 100007;
    int n, m;
    int main() {
        string s;
        cin >> s;
        n = s.size();
        int ans = 0;
        for(int j = 1; j < n; j++) {
            if(s[j-1] == 'V' && s[j] == 'K') ans++;
        }
    
        for(int i = 0; i < n; i++) {
            if(s[i] == 'V') s[i] = 'K';
            else if(s[i] == 'K') s[i] = 'V';
            int cnt = 0;
            for(int j = 1; j < n; j++) {
                if(s[j-1] == 'V' && s[j] == 'K') cnt++;
            }
            ans = max(cnt, ans);
            if(s[i] == 'V') s[i] = 'K';
            else if(s[i] == 'K') s[i] = 'V';
        }
        printf("%d
    ", ans);
        return 0;
    }
    View Code
  • 相关阅读:
    【bzoj1174】[Balkan2007]Toponyms Trie树
    【bzoj1786】[Ahoi2008]Pair 配对 dp
    【bzoj3956】Count 单调栈+可持久化线段树
    【bzoj4605】崂山白花蛇草水 权值线段树套KD-tree
    【bzoj3696】化合物 树形dp
    【bzoj1150】[CTSC2007]数据备份Backup 模拟费用流+链表+堆
    【bzoj3671】[Noi2014]随机数生成器 贪心
    【bzoj4653】[Noi2016]区间 双指针法+线段树
    【bzoj4197】[Noi2015]寿司晚宴 分解质因数+状态压缩dp
    用Python操作Named pipe命名管道,实用做法——os.read 或 os.write
  • 原文地址:https://www.cnblogs.com/cshg/p/6724521.html
Copyright © 2011-2022 走看看