CF 628C

  题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差,

       两个串的dis为各个位置上字符的dis之和,求和给定的字符串的dis为d的字符串,若含有多个则输出任意一个,不存在输出-1

  解题思路:简单贪心,按顺序往后,对每一个字符,将其变为与它dis最大的字符(a或者z),d再减去相应的dis,

       一直减到d为0,剩余的字母则不变直接输出。若一直到最后一位d仍然大于0,则说明不存在,输出-1.

/* CF 628C --- Bear and String Distance --- 简单贪心 */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ctype.h>
using namespace std; char a[]; int main()
{
#ifdef _LOCAL
freopen("D:\\input.txt", "r", stdin);
#endif int n, k;
while (scanf("%d%d", &n, &k) == ){
scanf("%s", a);
for (int i = ; i < n; ++i){
int t = max(a[i] - 'a', 'z' - a[i]);
if (k - t <= ){
if (islower(a[i] - k)){
a[i] -= k;
}
else{
a[i] += k;
}
k = ;
break;
}
else{
k -= t;
if (a[i] < ){
a[i] = 'z';
}
else{
a[i] = 'a';
} }
}//for(i)
if (k > ){
printf("-1\n");
}
else{
printf("%s\n", a);
} } return ;
}

下面是参考别人的代码写出来的,比较容易理解:

/* CF 628C --- Bear and String Distance --- 简单贪心 */
#include <cstdio>
#include <algorithm>
using namespace std; char a[]; int main()
{
int n, k;
while (scanf("%d%d", &n, &k) == ){
scanf("%s", a);
for (int i = ; i < n; ++i){
int dis1 = a[i] - 'a';
int dis2 = 'z' - a[i];
if (dis1 < dis2){
int ddd = min(k, dis2);
k -= ddd;
a[i] += ddd;
}
else{
int ddd = min(k, dis1);
k -= ddd;
a[i] -= ddd;
}
}//for(i)
k ? printf("-1\n") : printf("%s\n", a);
} return ;
}

CF 628C --- Bear and String Distance --- 简单贪心的更多相关文章

  1. Educational Codeforces Round 8 C. Bear and String Distance 贪心

    C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...

  2. Codeforces CF#628 Education 8 C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. codeforces 628C C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  4. CDOJ 1502 string(简单贪心)

    题目大意:原题链接 相邻两个字母如果不同,则可以结合为前一个字母,如ac可结合为a.现给定一个字符串,问结合后最短可以剩下多少个字符串 解体思路:简单贪心 一开始读题时,就联想到之前做过的一道题,从后 ...

  5. Uva 11729 Commando War (简单贪心)

    Uva 11729  Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...

  6. ACM_发工资(简单贪心)

    发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...

  7. ACM_Ruin of Titanic(简单贪心)

    Ruin of Titanic Time Limit: 2000/1000ms (Java/Others) Problem Description: 看完Titanic后,小G做了一个梦.梦见当泰坦尼 ...

  8. String Distance and Transform Process

    http://acm.hdu.edu.cn/showproblem.php?pid=1516 Problem Description String Distance is a non-negative ...

  9. CF 500 C. New Year Book Reading 贪心 简单题

    New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n b ...

随机推荐

  1. 《BI项目笔记》创建多维数据集Cube(1)

    有两个事实表,因此就有两个度量值组,并且向导将为非维度键的事实表中的每一个数值列创建一个度量值.由于我们这里不需要那么多,所以只选择部分度量值.另外要注意,度量值的名称源于事实表中的列,所有名称由可能 ...

  2. C++ do{...}while(0)的好处

    在开源软件里面经常可以看到这样的写法. #define X(a) do { f1(a); f2(a); } while(0) 1. 主要作用是放在宏定义里面,避免宏带来的语法问题. 比如 #defin ...

  3. D3.js 用层画条形图

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. CSS和Js样式属性的对照关系

    注意:1.在使用js时,如newTD.style.paddingLeft="200";(错误),正确生效的写法是newTD.style.paddingLeft="200p ...

  5. Android性能优化篇

    很多App都会遇到以下几个常见的性能问题: 启动速度慢:界面跳转慢:事件响应慢:滑动和动画卡顿. 一.启动速度优化. 优化初始化任务: 1. 把一些初始化任务懒加载初始化 2. 把初始化任务并行化(异 ...

  6. Samba Linux 和windows 共享

    1.安装Samba  (yum install Samba) 2.配置Samba  (Samba的配置文件为/etc/samba/smb.conf) 1)打开smb.conf   vim /etc/s ...

  7. js格式化数字和金额

    格式化数字,格式化金额: function number_format(number, decimals, dec_point, thousands_sep) { /* * 参数说明: * numbe ...

  8. a标签中的点击事件

    我们常用的在a标签中有点击事件:1. a href="javascript:js_method();" 这 是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问 ...

  9. Queue的push和front操作

    #include <queue> #include <cstdlib> using namespace std; int main(){ queue<int> qu ...

  10. Sprint.Net 笔记

    有生以来写的第一份博客, 还真不会写, 请高手们指导指导. 1.引入 Spring.Core.dll 和 Common.Logging.dll 两个文 2. 在UI层的Web.conf 的 <C ...