CodeForce1108C. Nice Garland (暴力+思维)
题意: 给出一个包含RGB三种颜色的序列, 要求改动最少的颜色, 使得所有相同元素之间的距离都是3的倍数
思路:仔细思考题意, 我们可以发现, 要想使得满足nice的条件, 只有两种可能
- 元素数量<3, 且互不相等
- 元素数量>=3, 则只能是 {“GRB”,“GBR”,“RGB”,“RBG”,“BGR”,“BRG”}, 种的一种重复出现
由此我们可以直接枚举这6种排列方式, 找出不相同元素最少的(即需要改动最少)一种
这道题运用到了对%的技巧, 值得学习
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const LL maxn = 1e6+10;
int main()
{
int len;
string s;
cin >> len >> s;
string tar[] = {"GRB","GBR","RGB","RBG","BGR","BRG"}; //一共6种排列方式
int ans = 1<<30, ansI = -1;
for(int i = 0; i < 6; i++){
int cnt = 0; //不相等字符的数量
for(int j = 0; j < len; j++)
if(tar[i][j%3]!=s[j]) cnt++;
if(cnt < ans)
ans = cnt, ansI = i;
}
cout << ans << endl;
for(int i = 0; i < len; i++)
cout << tar[ansI][i%3];
cout << endl;
return 0;
}