随便看看
查看原帖
随便看看
405001
TheSunInTheMorning楼主2020/11/17 21:00
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

string str ;
int a[1000] ;

int main() {
	int n, sum = 0, temp = 0, cnt1=1, cnt2,flag=0;
	
	cin >> n >> str;
	str = " " + str;
	cout << str << endl;
	while (  (temp=str.find("VK",temp+1))!=string::npos ) {
		a[temp] = 1;
		a[temp + 1] = 1;
		sum += 1;
	}   //在原数列中找VK并且标记在数组a中
	for (int i = 1; i <= n-1; ++i) {
		if (a[i] == 0 && a[i + 1] == 0) {
			
			if ( str[i] != "K" && str[i + 1] != "V" )
				flag = 1;
		} // 搜索数组a,当有两个连在一起的没被标识的字符判断是否符合条件。
	}

	if (flag == 1) cout << sum + 1;
	else cout << sum;

}

分享一下 , 首先找一下有多个VK ,记录在sum中,因为只需要变一个字符,所以最多增加一个。 显然两个连在一起的字符里除了 KV 都可以使得结果加一。

2020/11/17 21:00
加载中...