【CF#801 A.】 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.
InputThe 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.
OutputOutput 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 InputVK Output1 InputVV Output1 InputV Output0 InputVKKKKKKKKKVVVVVVVVVK Output3 InputKVKV Output1題目大意:給你一個(gè)字符串,改其中一個(gè)字母或者不改,然后讓你從中找有幾個(gè)VK。
解題報(bào)告:?? ? 思維題啊,找?guī)捉M數(shù)據(jù)測(cè)試一下就找出規(guī)律來(lái)了,就是先去掉VK的(記錄下來(lái)ans個(gè)),然后看有沒(méi)有VV或者KK,有的話(huà)就輸出ans+1,沒(méi)有的話(huà)就輸出ans
ac代碼:
#include<iostream> #include<cstdio> #include<cstring>using namespace std;const int N = 100 + 5; char a[N]; bool bk[N]; int ans = 0, flag = 0; int main() {cin>>a;int len = strlen(a);for(int i = 0; i < len; i++) {if(a[i] == 'V' && a[i + 1] == 'K') {ans++;bk[i]=1;bk[i+1]=1;}}for(int i = 0; i < len; i++) {if(bk[i]==0&&bk[i+1]==0&&a[i]==a[i + 1]) flag=1;}if(flag==1) ans++;printf("%d\n", ans);return 0 ; }總結(jié)
以上是生活随笔為你收集整理的【CF#801 A.】 Vicious Keyboard(字符串查找,水题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 视觉SLAM十四讲(3):三维空间刚体运
- 下一篇: 机器学习笔记(1):Introducti