Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西師大新生賽 L1-2.葉神的字符串


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.

 

 

這個題和師大的新生賽的一個題好像好像。。。

 

Jxnu Group Programming Ladder Tournament 2017

 

L1-2 葉神的字符串

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 23   Accepted Submission(s) : 10

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

眾所周知,ACS協會會長葉神學習特別好,算法能力也強,作為一個弱渣的大豪哥特別崇拜葉神,覺得‘Y’‘S’這兩個字符特別厲害,所以大豪哥的一個鍵盤上就只有Y,S兩個鍵,大豪哥用這個鍵盤打出了一個字符串s,但是他特別的不滿意,所以他想改變字符串s中的一個字符(也可以不改變),使得字符串s中可以截取出最大數量的“YS”

Input

多組輸入至文件結尾。
每組測試數據輸入一串由'Y','S'組成的字符串。(字符串長度最多為10000)

Output

輸出至多一次修改后最多有多少個“YS”

Sample Input

YYYS

Sample Output

2

唉,真是讓人無語。首先是這個師大的。

 

代碼1:

#include<stdio.h>
#include<string.h>
int main(){
    char a[10050];
    int i,flag[10050],len,num1,num2;
       while(gets(a)){
          len=strlen(a);
          for(i=0;i<len;i++)
            flag[i]=0;
            num1=0;
          for(i=1;i<len;i++){
            if(a[i]=='S'&&a[i-1]=='Y'){
                num1++;
                flag[i]=1;
                flag[i-1]=1;
            }
          }
            num2=0;
            for(i=1;i<len;i++){
                if(flag[i]==0&&flag[i-1]==0){
                       num2++;
                       flag[i]=1;     //其實這里去掉更好
                       flag[i-1]=1;   //樓上+1。。。 if(a[i]=='Y'&&a[i-1]=='S')
                        num2--;
                }
            }
        if(num2>=1)
            printf("%d\n",num1+1);
        else
            printf("%d\n",num1);
    }
    return 0;
}

 

修改了一下:

代碼2:

#include<stdio.h>
#include<string.h>
int main(){
    char a[10050];
    int i,flag[10050],len,num1,num2;
    while(gets(a)){
    len=strlen(a);
    for(i=0;i<len;i++)flag[i]=0;
    num1=0;
    memset(flag,0,sizeof(flag));
    for(i=1;a[i]!='\0';i++){
        if(a[i]=='S'&&a[i-1]=='Y'){
            num1++;
            flag[i]=1;
            flag[i-1]=1;
        }
    }
    num2=0;
    for(i=1;i<len;i++){
        if(flag[i]==0&&flag[i-1]==0){
            num2++;
            flag[i]=1;
            flag[i-1]=1;
            if(a[i]=='Y'&&a[i-1]=='S')num2--;
            else break;
        }
    }
    if(num2>=1)
        printf("%d\n",num1+1);
    else
        printf("%d\n",num1);
    }
    return 0;
}

 

其實還有一個代碼,但是少了個判斷條件,想法是一樣的,不貼了。。。

然后!!!在做了cf的這個類似題之后發現了新的問題。。。

等會再說吧,要貼cf的代碼了。。。

代碼1:

#include<stdio.h>
#include<string.h>
int main(){
    char a[105];
    int i,flag[105],len,num1,num2;
    while(gets(a)){
    len=strlen(a);
    for(i=0;i<len;i++)flag[i]=0;
    num1=0;
    memset(flag,0,sizeof(flag));
    for(i=1;a[i]!='\0';i++){
        if(a[i]=='K'&&a[i-1]=='V'){
            num1++;
            flag[i]=1;
            flag[i-1]=1;
        }
    }
    num2=0;
    for(i=1;i<len;i++){
        if(flag[i]==0&&flag[i-1]==0){
            num2++;        //這里和那個師大的不一樣了。 if(a[i]=='V'&&a[i-1]=='K')num2--;
            else break;
        }
    }
    if(num2>=1)
        printf("%d\n",num1+1);
    else
        printf("%d\n",num1);
    }
    return 0;
}

 

VKKVVVKVKVK這組數據如果改成師大的那個字母就wa了,因為flag[]那里錯了,(第二個循環里的標記去掉就可以了) 師大的數據水過了。。。

然后!!!cf的這個可以暴力寫。

代碼2:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=1005000;
char str[N];
char b[N];
int main(){
    while(gets(str)){
        int len=strlen(str);int maxx=0;
        if(len==1){cout<<0<<endl;continue;}
        for(int i=0;i<len-1;i++)if(str[i]=='V'&&str[i+1]=='K')
        maxx++;
        for(int i=0;i<len;i++){
            char c=str[i];int ans=0;
            if(str[i]=='V')str[i]='K';
            else str[i]='V';
            for(int j=0;j<len-1;j++){
                if(str[j]=='V'&&str[j+1]=='K')ans++;
            }
            maxx=max(maxx,ans);
            str[i]=c;
        }
        cout<<maxx<<endl;
    }
}

 

一開始有點沒太懂

以VKVKVKVVVV這個為例:第一次變成 KKVKVKVVVV,第二次 VVVKVKVVVV,還是3。。。VKKKVKVVVV ,VKVVVKVVVV,3

每次看他有幾個Vk,就這樣,最后取最大的,到后面的時候maxx=4,ans=4 所以還是4,但是用暴力寫師大的就會超時。

暴力寫時間復雜度是O(n^2),那個flag[]的時間復雜度是O(n),

因為師大的字符串長度是10000,所以暴力過不了,會超時。而cf的這個題是100,所以沒超時。。。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM