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. redis 数据导出

    一.导出所有的keys echo "keys 201*" |./redis-cli -h localhost -p 6379 -a password >> 1.txt ...

  2. 使用File类递归列出E盘下全部文件

    import java.io.File;public class FileListTest { public void tree(File file){ if(file.listFiles()!=nu ...

  3. Retrofit 2.0使用(2)如何使用@Body的形式发送Post

    在使用Retrofit的时候如果只是有几个参数我们可以用@Querry的形式,然后需要使用','隔开 但是在需要@Querry的参数多了之后,如果再用上面的方式就会造成参数写了一大堆的麻烦事 所以Re ...

  4. JSP动作标签

    动作标签是一种特殊的标签,它影响JSP运行时的功能. (1)include动作标签 <jsp:include page=”文件的名字” />           <%--   不可有 ...

  5. indent guides 格式化代码(添加竖线)

    点击 Visual Studio 2013 工具—扩展和更新—联机 然后输入indent guides 自动搜索出来这个插件(如图).注:Visual Studio 2010需要自己在网上下载安装. ...

  6. 线性表(一)——数组循环右移算法

    源码:rshift.cpp #include "stdafx.h" #include <stdio.h> /****************************** ...

  7. nodejs之process进程

    虽然node对操作系统做了很多抽象的工作,但是你还是可以直接和他交互,比如和系统中已经存在的进程进行交互,创建工作子进程.node是一个用于事件循环的线程,但是你可以在这个事件循环之外创建其他的进程( ...

  8. H5页面左图右边文字如何布局

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  9. MVC5 + EF6 入门完整教程一:从0开始

    第0课 从0开始 ASP.NET MVC开发模式和传统的WebForm开发模式相比,增加了很多"约定". 直接讲这些 "约定" 会让人困惑,而且东西太多容易忘记 ...

  10. iOS开发拓展篇—音频处理(音乐播放器2)

    iOS开发拓展篇—音频处理(音乐播放器2) 说明:该文主要介绍音乐播放界面的搭建. 一.跳转 1.跳转到音乐播放界面的方法选择 (1)使用模态跳转(又分为手动的和自动的) (2)使用xib并设置跳转 ...