CF Educational Round 12, C
2016-04-28 10:31
399 查看
C. Simple Strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ab, aba, zscoder are simple whereas aa, add are not simple.
zscoder is given a string s. He wants to change a minimum number of characters so that the string s becomes simple. Help him with this task!
Input
The only line contains the string s (1 ≤ |s| ≤ 2·105) — the string given to zscoder. The string s consists of only lowercase English letters.
Output
Print the simple string s’ — the string s after the minimal number of changes. If there are multiple solutions, you may output any of them.
Note that the string s’ should also consist of only lowercase English letters.
Examples
input
aab
output
bab
input
caaab
output
cabab
input
zscoder
output
zscoder
题意:
给一个字符串,要求通过最少的改变(每次只能改变一个字符)将其变成两两之间互不相等的字符串(有多种结果,只需要输出其中一种)
题解:
我的思路:首先两两比较,若相等,则将后面一个变换。使得变换后的字符不等于他的下一个字符(即aab->acb,而不是aab->abb)[好吧,自己在写的时候坑了自己,就是我是使用直接加减来改变,坑自己的点在y上(如果y后面是z,那么我的代码就使得y+了2.。。。于是就超出了范围。。。泪崩。。好吧,我还是觉得使用round比较好,,判断一下重来就行了)]
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ab, aba, zscoder are simple whereas aa, add are not simple.
zscoder is given a string s. He wants to change a minimum number of characters so that the string s becomes simple. Help him with this task!
Input
The only line contains the string s (1 ≤ |s| ≤ 2·105) — the string given to zscoder. The string s consists of only lowercase English letters.
Output
Print the simple string s’ — the string s after the minimal number of changes. If there are multiple solutions, you may output any of them.
Note that the string s’ should also consist of only lowercase English letters.
Examples
input
aab
output
bab
input
caaab
output
cabab
input
zscoder
output
zscoder
题意:
给一个字符串,要求通过最少的改变(每次只能改变一个字符)将其变成两两之间互不相等的字符串(有多种结果,只需要输出其中一种)
题解:
我的思路:首先两两比较,若相等,则将后面一个变换。使得变换后的字符不等于他的下一个字符(即aab->acb,而不是aab->abb)[好吧,自己在写的时候坑了自己,就是我是使用直接加减来改变,坑自己的点在y上(如果y后面是z,那么我的代码就使得y+了2.。。。于是就超出了范围。。。泪崩。。好吧,我还是觉得使用round比较好,,判断一下重来就行了)]
#include<iostream> #include<string> #include<stdio.h> using namespace std; int main(){ string s; cin>>s; for(int i=1;i<s.size();){ if(i==s.size()-1) { if(s[i-1]==s[i]) { if(s[i]=='z'){s[i]-=1;} else if(s[i]=='a'){s[i]+=1;} else{s[i]+=1;} } break; } if(s[i]==s[i-1]){ if(s[i]=='z') { s[i]-=1; if(s[i+1]==s[i]) {s[i]-=1;} } else if(s[i]=='a') { s[i]+=1; if(s[i+1]==s[i]) {s[i]+=1;} } else { s[i]+=1; if(s[i+1]==s[i]) {s[i]+=1;if(s[i]>'z')s[i]-=3;} } i+=2;continue; } i++; } cout<<s<<endl; return 0; }
相关文章推荐
- ListView中嵌套GridView,监听事件发生冲突。
- 欢迎使用CSDN-markdown编辑器
- Python 元组
- 判断ListView的第一个/最后一个item是否完全显示
- (转)Android Eclipse 代码混淆
- setInterval最大时间
- iOS-App的启动页设置
- hibernate 数据缓存
- Win7系统开机进入欢迎界面出现黑屏的解决方法
- PHP安装配置及解决方案
- Hive 查询中的分组排名
- IOS工程配置
- Docker计划将镜像迁移至Alpine Linux平台,简化容器运行环境
- Android中手势滑动翻页之GestureDetector总结
- 浅谈下拉放大
- 内存动态分配管理
- 自制力差的元凶及习惯的养成过程
- 栈帧、局部变量表、操作数栈
- Annotation总结
- iOS App设置icon,启动图,App名称的方法