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. 试用VSCode

    VSCode是代码编辑器,不是IDE. 优点: 1.内置ES6代码高亮和提示,语法验证 2.除了支持到变量定义处Go to definition,还支持弹框显示变量定义出peek definition ...

  2. CSS3之尖角标签

    如图所示,Tag标签的制作通常使用背景图片,现在用CSS3代码就能实现尖角效果(需浏览器支持CSS3属性). 运用CSS3样式实现尖角标签,只需要写简单的HTML结构和CSS样式. <p> ...

  3. Android开发之各个语言

    Android开发之各个语言 1.进行源码开发遇到一个最基础的问题就是各个语言下的字串翻译,所以我们必须得清楚res文件夹下各个资源文件夹 2.如图:

  4. Software Engineering: 2. Project management

    resources:"Software Engineering" Ian Sommerville For most projects, important goals are: D ...

  5. swig之于c++

    [namespace] namespace nsTest1 { int nsAdd(int a, int b) { return a + b; } } namespace nsTest2 { int ...

  6. linux 查看所有用户

    cat /etc/passwd | cut -f -d:

  7. Using Spring Boot without the parent POM

    Using Spring Boot without the parent POM: 问题 spring boot项目一般情况下的parent如下: <parent> <groupId ...

  8. Windows与Linux主机之间的连接和交互工具

    1.Putty 远程连接Linux主机 Windows主机上安装putty,工具打开后显示如下: 输入要连接的Linux主机的IP地址,点击Load,连接主机后输入用户名密码,即可登录Linux主机 ...

  9. 解决Ubuntu 下 vi编辑器不能使用方向键和退格键问题

    转自:http://blog.csdn.net/sky101010ws/article/details/51012103 使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多字母 这个问题主要是 ...

  10. redis服务和扩展安装(windows)

    Windows下安装redis和在php中使用phpredis扩展 原文地址:http://m.oschina.net/blog/281058 Junn 发布于 2年前,共有 0 条评论 1.redi ...