CodeForces 219C Color Stripe (贪心)
来源:互联网 发布:vb.net string转int 编辑:程序博客网 时间:2024/05/02 01:41
题意:
用前k个大写字母,替代原来字符串的大写字母,使得两两相邻的字母不一样。要求使用最少的替代次数。
思路:
因为是在dp专题里看到,所以一直想dp的做法,后来实在不知道应该怎么搞。
于是乎看了下别人的思路。
k > 2:直接模拟枚举下去。每个位置与两旁的字母比较,相同就改,做到不与左右相同即可。(证明不会= =,应该是贪心吧)
k = 2:只有两种情况,ABABA……,BABABA……哪种改动次数少选哪种。
code:
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN = 5*1e5+5;const int INF = 0x3f3f3f3f;int n, k;char ch[MAXN];char tt[MAXN];void spdeal() { int len = strlen(ch); strcpy(tt, ch); int t = 0; int res = 0; for(int i = 0;i < len; i++) { if(ch[i] != t+'A') { ch[i] = t+'A'; res ++; } t ^= 1; } int res2 = 0; t = 1; for(int i = 0;i < len; i++) { if(tt[i] != t+'A') { tt[i] = t+'A'; res2++; } t ^= 1; } printf("%d\n", min(res, res2)); if(res < res2) printf("%s\n", ch); else printf("%s\n", tt);}void solve() { if(k == 2) { spdeal(); return; } int len = strlen(ch); int res = 0; for(int i = 1;i < len; i++) { if(i == len-1) { if(ch[i-1] == ch[i]) { for(int t = 0;t < k; t++) if(t+'A' != ch[i-1]) { res++; ch[i] = t+'A'; break; } } } else { if(ch[i-1]==ch[i]) { for(int t = 0;t < k; t++) { char tmp = t+'A'; if(tmp != ch[i-1] && tmp != ch[i+1]) { res++; ch[i] = tmp; break; } } } } } printf("%d\n", res); printf("%s\n", ch);} int main() { scanf("%d%d", &n, &k); scanf("%s", ch); solve(); return 0;}
0 0
- CodeForces 219C Color Stripe (贪心)
- codeforces 219C Color Stripe(贪心)
- Codeforces 219C Color Stripe 贪心
- CodeForces 219C Color Stripe
- Color Stripe--codeForces 219C
- codeforces 219C Color Stripe
- codeforces C. Color Stripe
- codeforces 219C C. Color Stripe(dp)
- Codeforces 219C C. Color Stripe【dp+输出路径】
- 219C - Color Stripe-模拟
- Codeforces Round #135 (Div. 2) C. Color Stripe(字符串dp)
- Codeforces Round #135 (Div. 2)C. Color Stripe
- Codeforces Round #135 (Div. 2)-C. Color Stripe
- codeforces - 18C - Stripe(练习)
- CodeForces 18C - Stripe
- CodeForces 21 C Stripe 2
- CodeForces 21C Stripe 2 (简单题)
- CodeForces 18C Stripe (简单题)
- hdu 1317 XYZZY 国产SPFA算法AC,,题目输入部分真特么难懂。。内有解析
- Android更新UI的几种方法
- nyoj119士兵杀敌(三)
- Cannot change deployment state from ERROR to REDEPLOYING.
- Harris角点检测
- CodeForces 219C Color Stripe (贪心)
- linux下配置,启动多个tomcat
- java类集框架(Collection)(List)
- 《曾国藩家书》读书手记(修身篇一)
- POJ2914 Minimum Cut【全局最小割】【Stoer-Wangner】
- C#中几种反射机制的比较
- PHP中字符串的heredoc结构和nowdoc结构
- 过年随笔
- poj2955 Brackets (区间dp)