Codeforces CF#628 Education 8 C. Bear and String Distance
1 second
256 megabytes
standard input
standard output
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 usegets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead ofScanner/System.out in Java.
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.
If there is no string satisfying the given conditions then print "-1" (without the quotes).
Otherwise, print any nice string s' that
.
4 26
bear
roar
2 7
af
db
3 1000
hey
-1
题意:
给出一个长度为n的字符串,定义两个字符串的距离就是对应位的字母在字母表上的距离之和,也就是ae跟ea距离为4+4=8。
给出k,要求构造一个等长的字母串,使得两串距离恰好为k。
题解:
很容易想到每一位都尽量拉长距离,尽快达到k,然后全部一样即可。
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
int n, k;
char str[N];
char ans[N]; int main() {
scanf("%d%d", &n, &k);
scanf("%s", str);
memset(ans, , sizeof(ans));
for(int i = ; i < n; ++i) {
int dis = min(k, max(str[i] - 'a', 'z' - str[i]));
if(str[i] - 'a' >= dis) ans[i] = str[i] - dis;
else ans[i] = str[i] + dis;
k -= dis;
}
if(k) puts("-1");
else printf("%s\n", ans);
return ;
}
Codeforces CF#628 Education 8 C. Bear and String Distance的更多相关文章
- Codeforces CF#628 Education 8 F. Bear and Fair Set
F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces CF#628 Education 8 E. Zbazi in Zeydabad
E. Zbazi in Zeydabad time limit per test 5 seconds memory limit per test 512 megabytes input standar ...
- Codeforces CF#628 Education 8 D. Magic Numbers
D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces CF#628 Education 8 B. New Skateboard
B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces CF#628 Education 8 A. Tennis Tournament
A. Tennis Tournament time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
- CF 628C --- Bear and String Distance --- 简单贪心
CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...
- 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 ...
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
随机推荐
- django(一)
Django 自称是"最适合开发有限期的完美WEB框架".本文参考<Django web开发指南>,快速搭建一个blog 出来,在中间涉及诸多知识点,这里不会详细说明, ...
- css3动画第一式--简单翻滚
在w3cschool上面查阅css3的动画语法手册时,发现“css3 动画”栏目首页放了一个翻滚的div动画案例,觉得挺好看的,于是就自己模仿着写了一下,感觉还行O(∩_∩)O哈哈~ 查看原地址 下面 ...
- BOM 浏览器对象模型
总结自JavaSript高级编程
- 第二篇 基于.net搭建热插拔式web框架(沙箱的构建)
上周五写了一个实现原理篇,在评论中看到有朋友也遇到了我的问题,真的是有种他乡遇知己的感觉,整个系列我一定会坚持写完,并在最后把代码开源到git中.上一篇文章很多人看了以后,都表示不解,觉得不知道我到底 ...
- fastcgi与cgi的区别
fastcgi与cgi的区别 先讲下cgi:cgi在2000年或更早的时候用得比较多, 以前web服务器一般只处理静态的请求,如果碰到一个动态请求怎么办呢?web服务器会根据这次请求的内容,然后会fo ...
- Gerrit增加SSL证书
在http的基础上增加SSL 配置gerrit.config文件 [gerrit] basePath = git canonicalWebUrl = https://172.16.99.212/ .. ...
- Gitlab完美安装【CentOS6.5安装gitlab-6.9.2】
摘要: 拆腾了几天,终于在今天找到了快速安装Gitlab的方法.CentOS6.5安装gitlab-6.9.2 参考网址:https://gitlab.com/gitlab-org/omnibus-g ...
- Sublime 3 如何配置SVN插件
在sublime里面安装svn的插件,就可以在sublime的操作界面里面进行相关svn操作,这样就不用再回到文件系统中,进行相关svn的操作. 1.在进入sublime界面后,点击顶部菜单“Pref ...
- 最终版的Web(Python实现)
天啦,要考试了,要期末考试了,今天把最终版的Python搭建Web代码先写这里记下了.详细的过程先不写了. 这次是在前面的基础上重写 HTTPServer 与 BaseHTTPRequestHandl ...
- Ubuntu16.04 VTK7.1.0+QT4.8.6+QtCreator开发环境配置
VTK需要OpenGL3.0或更高版本的驱动,但虚拟机下的Ubuntu不支持OpenGL3.0,或者自己按网上教程配置之后也能支持,但过程相当繁琐,本人试验失败. 最终决心采用双系统,装好之后,执行g ...