求调
查看原帖
求调
934977
lichengxi1楼主2024/8/16 11:56
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 5e3 + 10;
int n,a[MAXN],d[MAXN],s[MAXN],ansk,ansm = 1e9;
char ch;

int work(int k){
	int D = 0,cnt = 0;
	memcpy(s,d,sizeof(d));
	for(int i = 1;i <= n;++i){
		D ^= s[i];
		if(D == 0){
			if(i + k - 1 > n) return -1;
			s[i] ^= 1,s[i + k] ^= 1;
			cnt++;
		}
	}
	return cnt;
}

int main(){
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 1;i <= n;++i){
		cin >> ch;
		if(ch == 'F') a[i] = 1;
		else a[i] = 0;
		d[i] = a[i] ^ a[i - 1];
	}
	for(int k = 1;k <= n;++k){
		int res = work(k);
		if(res == -1) continue;
		if(ansm > res){
			ansm = res,ansk = k;
		}
	}
	cout << ansk << ' ' << ansm << endl;
	return 0;
}

蒟蒻的代码,各位大佬麻烦调一下,谢谢

思路

d数组表示原先的a数组的差分,不知道哪里错了

2024/8/16 11:56
加载中...