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

题意:

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+4;
char s[N];
int n,k;
int main()
{
int num=0;
scanf("%d%d",&n,&k);
scanf("%s",s);
for(int i=0;i<n;i++)
{
num+=max(s[i]-'a','z'-s[i]);
}
if(k>num)cout<<"-1"<<"\n";
else
{
int sum;
for(int i=0;i<n;i++)
{
sum=max(s[i]-'a','z'-s[i]);
//cout<<sum<<endl;
if(sum<=k){
k-=sum;
if(s[i]-'a'>'z'-s[i])s[i]='a';
else s[i]='z';}
else if(sum>k&&k!=0)
{
if(s[i]-'a'>=k)
{
s[i]=s[i]-k;
}
else
{
s[i]=s[i]+k;
}
k=0;
}
printf("%c",s[i]);
} }
return 0;
}

codeforces 628C C. Bear and String Distance的更多相关文章

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

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

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

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

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

  4. String Distance and Transform Process

    http://acm.hdu.edu.cn/showproblem.php?pid=1516 Problem Description String Distance is a non-negative ...

  5. codeforces 680C C. Bear and Prime 100(数论)

    题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题

    http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...

  7. codeforces 653B B. Bear and Compressing(dfs)

    题目链接: B. Bear and Compressing time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  8. codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)

    题目链接: C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes ...

  9. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

随机推荐

  1. UFLDL深度学习笔记 (三)无监督特征学习

    UFLDL深度学习笔记 (三)无监督特征学习 1. 主题思路 "UFLDL 无监督特征学习"本节全称为自我学习与无监督特征学习,和前一节softmax回归很类似,所以本篇笔记会比较 ...

  2. KVM和QEMU的关系(转载)

    From:http://blog.sina.com.cn/s/blog_605f5b4f0102uyjv.html KVM是一种基于CPU硬件辅助的全虚拟化技术,没有CPU硬件虚拟化的支持,KVM无法 ...

  3. Dispose模式释放非托管资源

    实现方式用的是设计模式里的模板模式,基类先搭好框架,子类重写void Dispose(bool disposing) 即可. 需要注意的是基类的Finalize函数也就是析构函数调用的是虚函数void ...

  4. ios NavigationViewController跳转以及返回传值

    (一)使用NavigationViewController进行页面跳转时,应该使用pushViewController方法来跳转至下一页面.这种话.下一页面相同在NavigationViewContr ...

  5. Unity Editor Inspector编辑模板

    效果图: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEdito ...

  6. Tomcat startup.bat启动隐藏弹出的信息窗口

    to make tomcat to use javaw.exe instead of java.exe using some startup parameter or environment vari ...

  7. iOS开发---业务逻辑

    iOS开发---业务逻辑   1. 业务逻辑 iOS的app开发的最终目的是要让用户使用, 用户使用app完成自己的事就是业务逻辑, 业务逻辑的是最显眼开发工作.但是业务逻辑对于开发任务来说, 只是露 ...

  8. List和Set排序的实现

    List.Set.Map的区别 List和Set继承了Collection接口. List以特定索引来存取元素,可以有重复元素.Set不能存放重复元素(用对象的equals()方法来区分元素是否重复) ...

  9. Q: Why can't I access the Site Settings of my SharePoint site? 'File Not Found'

    Q: I am trying to access the Site Settings of my SharePoint site, but I get a File Not Found error, ...

  10. URAL 1181 Cutting a Painted Polygon【递归+分治】

    题目: http://acm.timus.ru/problem.aspx?space=1&num=1181 http://acm.hust.edu.cn/vjudge/contest/view ...