文章
68
粉丝
691
获赞
26
访问
578.4k
https://blog.csdn.net/csyifanZhang/article/details/106391657
推导过程↑
#define ll long long
#define vec vector<int>
#define inf 0x3f3f3f3f
#define MAX 100005
#define P pair<ll,ll>
#define MOD 1000000
int main() {
string s;
int s1[MAX], s2[MAX], s3[MAX];//到了第i个位置,一共出现了s[i]次R/G/B
while (cin >> s) {
memset(s1, 0, sizeof(s1));
memset(s2, 0, sizeof(s2));
memset(s3, 0, sizeof(s3));
int len = s.size(), res = inf;
for (int i = 0; i < len; i++) {
int a = 0, b = 0, c = 0;
if (s[i] == 'R')a++;
else if (s[i] == 'G')b++;
else c++;
s1[i + 1] = s1[i] + a;
s2[i + 1] = s2[i] + b;
s3[i + 1] = s3[i] + c;
}
//通过i,j将串分成三份,第一段保留R,第二段保留G,第三段保留B
for (int i = 0; i <= len; i++) {
for (int j = i; j <= len; j++) {
int cnt = s1[i] + s2[j] - s2[i] + s3[len] - s3[j];
if (len - cnt < res)
res = len - cnt;
}
}
cout << res << endl;
}
}
登录后发布评论
暂无评论,来抢沙发