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. C#综合笔记

    AspNetPager分页控件 UrlPaging="true" 利用get方式page?=1进行分页. UrlPaging="false"利用post方式进行 ...

  2. Beginning Windows Azure Development Guide

    目  录 一 初始化Windows Azure 二 云应用程序的编写. 2.1云应用程序的创建. 2.2一个简单的云应用程序. 2.3 托管云程序. 三 云程序的数据库操作. 3.1通过Cloud平台 ...

  3. iOS - Library 库

    1.动态库 & 静态库 什么是库: 库是程序代码的集合,是共享程序代码的一种方式.根据源代码的公开情况,库可以分为 2 种类型: 开源库: 公开源代码,能看到具体实现. 比如 SDWebIma ...

  4. spring主要的作用?

    在SSH框假中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL语句.Struts是用来 ...

  5. java jar包解析:打包文件,引入文件

    java jar包解析:打包文件,引入文件 cmd下: jar命令:package包打包 javac命令:普通类文件打包 Hello.java: package org.lxh.demo; publi ...

  6. SPSS常用基础操作(1)——变量分组

    有时我们需要对数据资料按照某个规则进行归组,如 在上述资料中,想按照年龄进行分组,30岁以下为组1,30-40岁为组2,40岁以上为组3 有两种方法可以实现: 1.使用计算变量功能 <1> ...

  7. css学习笔记 2

    css中的简写: 颜色的简写有三种:十六进制的形式.rgb(a).颜色名称. 单位的省略:当属性值为0px时,可以简写为0. margin和padding的简写,不多说了. border的简写:bor ...

  8. 博客打开慢?请禁用WordPress默认的谷歌字体!

    最近几天,谷歌中国挂了之后,发现我的博客打开极慢,原以为是空间问题,可一查,发现同台服务器的用户打开并不慢,排除了空间问题后,这边查询元素发现博客打开时加载了一个链接地址“fonts.googleap ...

  9. net中序列化读写xml

    参考http://www.cnblogs.com/fish-li/archive/2013/05/05/3061816.html 我们可以直接使用XmlTextReader.XmlDocument.X ...

  10. CodeForces #369 div2 D Directed Roads DFS

    题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...