【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題目大意:給你一個字符串,改其中一個字母或者不改,然后讓你從中找有幾個VK。
解題報告:?? ? 思維題啊,找幾組數據測試一下就找出規律來了,就是先去掉VK的(記錄下來ans個),然后看有沒有VV或者KK,有的話就輸出ans+1,沒有的話就輸出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 ; }總結
以上是生活随笔為你收集整理的【CF#801 A.】 Vicious Keyboard(字符串查找,水题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视觉SLAM十四讲(3):三维空间刚体运
- 下一篇: JAMMER2ND.EXE是什么进程 J