描述
题解
一个模拟问题,O(n)解,先查找所有不用置换的VK
组合数,然后能够通过一次置换得到VK
的组合只有出现连着的两个字符相同,而这里又需要注意的是,两个V
相连时,如果再往后接着K
那么就不能算是可以置换的,而只能算是一个V
+一个不用置换的VK
,这样也就 AC 了。这个问题稍微有一些贪心的意味~~~
代码
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
string s;
while (cin >> s)
{
if (s.length() == 1)
{
cout << 0 << '\n';
continue;
}
int res = 0, flag = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == 'V' && s[i + 1] == 'K')
{
res++;
i++;
}
else if ((s[i] == 'V' && s[i + 1] == 'V' && s[i + 2] != 'K')
|| (s[i] == 'K' && s[i + 1] == 'K'))
{
flag = 1;
i++;
}
}
if (flag)
{
res++;
}
cout << res << '\n';
}
return 0;
}