Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance
题目连接:
http://www.codeforces.com/contest/628/problem/C
Description
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.
The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .
Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .
Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106).
The second line contains a string s of length n, consisting of lowercase English letters.
Output
If there is no string satisfying the given conditions then print "-1" (without the quotes).
Otherwise, print any nice string s' that .
Sample Input
4 26
bear
Sample Output
roar
Hint
题意
现在定义了一个dis(a,b),dis(a,b) = abs(a-b),等于这两个数的ASCII码的距离
然后现在给你一个串,让你构造另外一个串,使得这两个串之间的距离和恰好等于k
题解:
贪心,我们暴力向最远的地方靠就好了
代码
#include<bits/stdc++.h>
using namespace std;
string s,s1;
int main()
{
int n,k;
cin>>n>>k;
cin>>s;
for(int i=0;i<s.size();i++)
{
int dis1 = 'z'-s[i];
int dis2 = s[i]-'a';
if(dis1>dis2)
{
int ddd = min(dis1,k);
k-=ddd;
s1+=s[i]+ddd;
}
else
{
int ddd = min(dis2,k);
k-=ddd;
s1+=s[i]-ddd;
}
}
if(k)return puts("-1");
else cout<<s1<<endl;
}
Educational Codeforces Round 8 C. Bear and String Distance 贪心的更多相关文章
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
- Educational Codeforces Round 8 F. Bear and Fair Set 最大流
F. Bear and Fair Set 题目连接: http://www.codeforces.com/contest/628/problem/F Description Limak is a gr ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- Educational Codeforces Round 40 I. Yet Another String Matching Problem
http://codeforces.com/contest/954/problem/I 给你两个串s,p,求上一个串的长度为|p|的所有子串和p的差距是多少,两个串的差距就是每次把一个字符变成另一个字 ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some or ...
- Educational Codeforces Round 62 (Rated for Div. 2) C 贪心 + 优先队列 + 反向处理
https://codeforces.com/contest/1140/problem/C 题意 每首歌有\(t_i\)和\(b_i\)两个值,最多挑选m首歌,使得sum(\(t_i\))*min(\ ...
随机推荐
- python实战===图片转换为字符的源码(转)
#cmd执行命令为>>> python xx.py pic.png#-*- coding:utf-8 -*- from PIL import Image import argpars ...
- 各种好用的代码生成器(C#)
各种好用的代码生成器(C#) 1:CodeSmith 一款人气很旺国外的基于模板的dotnet代码生成器 官方网站:http://www.codesmithtools.com 官方论坛:http:// ...
- linux命令(21):more命令
实例1:显示文件中从第3行起的内容 [root@host-172-168-80-55 home]# cat test.log aaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb ...
- SQL Server 2008的MSSQLSERVER 请求失败或服务未及时响应
我的是SQL server 2008R2, 以前可以正常的启动SQL server(SQLEXPRESS).SQL server(MSSQLSERVER),有几天没有打开了,就在昨天 开机之后就无法启 ...
- Efficient Graph-Based Image Segmentation
转:http://blog.csdn.net/asongoficeandfire/article/details/8434799 Efficient Graph-Based Image Segment ...
- python读写xml文件
python读取xml文件 xml文件是具有树状结构的,如果想要访问某个叶子结点,必须逐层获取其父结点,要读取某个叶子结点内容用text成员 使用前先加载xml工具包 try: import xml. ...
- 阿里云轻量应用服务器——配置MySQL远程连接(踩坑,LAMP+CentOS)
说在前面 本文讲解清晰,从0开始 如不能用Navicat等数据库软件远程登陆,请先检查:安全>防火墙中 是否添加了MYSQL的3306端口(ECS服务器请检查 安全组)如未添加,先点右上角“添加 ...
- C#读取RSS源,并利用Solr索引
折磨我几天的问题今天终于解决了,分享一下近期solr使用的一些经验. 本来是用nutch在爬取页面,可是客户需要爬取RSS,而且可以识别那些页面是通过RSS源抓取出来的.nutch虽然自带解析RSS的 ...
- Windows 10 安装 Mongodb
因为新换了Windows 10 电脑,需要在新电脑重新安装所有的软件,包括mongodb 下载文件:首先在mongodb的官方网站上下载最新版本的mongodb安装程序,https://www.mon ...
- firfox与about:config
¤什么是about:config¤about:config是Firefox的设置页面,Firefox提供了不少高级设置选项在这里以便让你可以更加详细地控制Firefox的运行方式.官方不推荐用户手工修 ...