Educational Codeforces Round 12(C)贪心
来源:互联网 发布:数控编程待遇 编辑:程序博客网 时间:2024/04/29 08:01
题意:给你一个字符串,让你修改最少得次数,使得这个字符串相邻的字符不相同
题解:贪心就好啦, 使用前缀和扫出相同字符子串的最大长度(注意只有相同的字符才继承前面的值,否则就从1开始计数),然后每2个字符修改一次,因为一个字符最少的修改次数明显是(n/2)
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<string>#include<bitset>#include<utility>#include<functional>#include<iomanip>#include<sstream>#include<ctime>using namespace std;#define N int(1e6)#define inf int(0x3f3f3f3f)#define mod int(1e9+7)typedef long long LL;char s[N];int pre[N];vector<pair<int, int> >pos;int main(){#ifdef CDZSCfreopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout);int _time_jc = clock();#endifwhile (~scanf("%s", s)){pre[strlen(s)] = 1;pos.clear();pre[0] = 1;for (int i = 1; s[i]; i++){pre[i] = 1;if (s[i] == s[i - 1]){pre[i] = pre[i - 1] + 1;}}for (int i = 0; s[i]; i++){if (pre[i] > 1 && pre[i + 1] == 1){pos.push_back(make_pair(pre[i], i));}}for (int i = 0; i < pos.size(); i++){int x = pos[i].first;for (int j = pos[i].second - x + 2; j <= pos[i].second; j += 2){char c = 'a';while (c == s[j + 1] || c == s[j - 1]){c++;}s[j] = c;}}printf("%s\n", s);}return 0;}
0 0
- Educational Codeforces Round 12(C)贪心
- Codeforces Educational Codeforces Round 3 C. Load Balancing(贪心)
- Educational Codeforces Round 12 C. Simple Strings 贪心
- Educational Codeforces Round 2C. Make Palindrome(贪心构造)
- Educational Codeforces Round 6(C)尺取法+贪心
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- Educational Codeforces Round 5(B)贪心
- Educational Codeforces Round 6 (A)贪心
- Educational Codeforces Round 2 C.Make Palindrome(贪心)
- Educational Codeforces Round 6 C. Pearls in a Row(贪心)
- Educational Codeforces Round 21 808C Tea Party 【贪心算法】
- Educational Codeforces Round 21 C. Tea Party 贪心
- Educational Codeforces Round 22-C. The Tag Game-搜索,贪心
- Educational Codeforces Round 25 C Multi-judge Solving 贪心
- Educational Codeforces Round 12 C.Simple Strings
- CF Educational Codeforces Round 15(C)
- Educational Codeforces Round 6-C. Pearls in a Row(贪心)
- Educational Codeforces Round 8 C. Bear and String Distance(贪心)
- 如何在windows系统的cmd命令行下调用VS编译器
- Python Matplotlib库入门指南
- TLD算法的几句话总结
- javascript:算法笔记
- apache mesos 入门(3)——自定义framework + executor
- Educational Codeforces Round 12(C)贪心
- was compiled with optimization - stepping may behave oddly; variables may not be available.
- java“找不到或无法加载主类”及编码错误问题
- Maven 配置tomcat 插件运行
- 多个域名使用微信获取用户信息的解决办法
- Spring+SpringMVC+Mybatis 跳转流程 :
- 数据库主外键设计
- swift 学习计划(一)元组、Optional可选类型
- 解决 /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwi