C. Bear and String Distance
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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 .

Examples
input
4 26
bear
output
roar
input
2 7
af
output
db
input
3 1000
hey
output
-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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Educational Codeforces Round 8 C. Bear and String Distance 贪心

    C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...

  7. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  8. 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 ...

  9. Codeforces 915E Physical Education Lessons

    原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...

随机推荐

  1. java基础 绘图技术.坦克大战 之java绘图坐标体系(二)

    功能:在坐标系上绘制坦克 /* * 功能:坦克游戏的1.0 * 1. 画出坦克 * */ package com.tank; import javax.swing.*; import java.awt ...

  2. 云服务器 Centos7.0 部署

    CentOS安装jdk的三种方法 http://www.mamicode.com/info-detail-613410.html centos Linux下安装Tomcat和发布Java的web程序 ...

  3. WinForm 进程、线程、TreeView递归加载、发送邮件--2016年12月13日

    进程:一个程序就是一个进程,但是也有一个程序需要多个进程来支持的情况 进程要使用的类是:Process它在命名空间:System.Diagnostics; 静态方法Start(); Process.S ...

  4. sublime自动生成头部注释

    1.在tool->new snippet-创建一个新的snippet sublime text2 用snippet 创建文件头部信息 Snippets are smart templates t ...

  5. org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [XXX] in DispatcherServlet with name 'springMVC'

    在web.xml中添加 <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern ...

  6. selenium webdriver 建行软键盘输入密码

    driver.get("https://ibsbjstar.ccb.com.cn/app/V5/CN/STY1/login.jsp"); driver.manage().timeo ...

  7. 记录SQL Server2008日志文件损坏的恢复过程

    记录SQL Server2008日志文件损坏的恢复过程: 环境: 系 统:Windows Server2003 数据库:SQL Server2008 故障原因: 通过mstsc链接同一服务器时,用户界 ...

  8. B+Tree和MySQL索引分析

    首先区分两组概念: 稠密索引,稀疏索引: 聚簇索引,非聚簇索引: btree和mysql的分析: 参见 http://blog.csdn.net/hguisu/article/details/7786 ...

  9. maven学习(中)- 私服nexus搭建

    接上回继续,相信大家对maven每次都联网从国外站点下载依赖项的龟速网络已经不坎忍受了,今天先来看看如何搭建"仓库私服",目前nexus是使用比较广泛的私服软件之一,下面将介绍基本 ...

  10. 如何搞定IE+google双内核的360浏览器表单自动回填兼容问题

    最近开发中碰到一个关于表单问题,在用户提交表单时候浏览器会提示是否保存帐号 如果点击保存,在退出帐号切换其他帐号时,浏览器会自动为表单填充数据,为了解决这个自动填充问题时, 主要分2个思路来解决,一个 ...