zoukankan      html  css  js  c++  java
  • B

    B - Vicious Keyboard 

    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.

    Example

    Input
    VK
    Output
    1
    Input
    VV
    Output
    1
    Input
    V
    Output
    0
    Input
    VKKKKKKKKKVVVVVVVVVK
    Output
    3
    Input
    KVKV
    Output
    1
    题意:输入一个字符串,输出其中有多少对VK,可以任意更改其中的一个字符串使VK对的数量最多。
    题解:暴力先不更改计算有多少对VK,然后没次更改一个字符串计算一次,取最大值。
    代码
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    using namespace std;
    int main()
    {
        char str[105],s;
        int ans[210],i,j,len;
        while(cin>>str){
               memset(ans,0,sizeof(ans));
            len=strlen(str);
            for(i=0;i<len;i++)
                if(str[i]=='V'&&str[i+1]=='K'&&(i+1)<len)
                ans[0]++;
            for(j=1;j<=len;j++)//依次把字符串换成K计算,每次换完后要换回去。
            {
                s=str[j-1];
                str[j-1]='K';
                    for(i=0;i<len;i++)
                if(str[i]=='V'&&str[i+1]=='K'&&(i+1)<len)
                ans[j]++;
                str[j-1]=s;
            }
            for(j=len+1;j<=2*len;j++)//依次把字符串换成V计算
            {
                s=str[j-1-len];
                str[j-1-len]='V';
                for(i=0;i<len;i++)
                if(str[i]=='V'&&str[i+1]=='K'&&(i+1)<len)
                ans[j]++;
                str[j-1-len]=s;
            }
            ans[0]=*max_element(ans,ans+2*len+1);
            cout<<ans[0]<<endl;
        }
    }
    
    
    
    
    
  • 相关阅读:
    2019/5/13 洛谷P4742 【tarjan缩点 + 拓扑dp】
    图论500题
    欧拉回路与欧拉路径
    二分图的判定
    二分图的最大匹配以及带权匹配【匈牙利算法+KM算法】
    网络流三大算法【邻接矩阵+邻接表】POJ1273
    马拉车算法,mannacher查找最长回文子串
    tarjan算法(强连通分量 + 强连通分量缩点 + 桥(割边) + 割点 + LCA)
    luogu P5774 [JSOI2016]病毒感染 线性 dp
    luguo P2519 [HAOI2011]problem a dp+贪心
  • 原文地址:https://www.cnblogs.com/GXXX/p/6813591.html
Copyright © 2011-2022 走看看