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. Android 中通过切割图片创建人物行走动画

    以前一直使用序列图片来实现动画效果,造成空间的极大浪费,所以想要尝试下切割图片来实现动画. 如图所示,是由66rpg纸娃娃系统生成的角色行走图.本程序必须实现将人物的整体图片切割后存入4x4的数组来动 ...

  2. 2016 acm香港网络赛 A题. A+B Problem (FFT)

    原题地址:https://open.kattis.com/problems/aplusb FFT代码参考kuangbin的博客:http://www.cnblogs.com/kuangbin/arch ...

  3. github 答题

    头脑王者 / 百万英雄 / 冲顶大会 / 芝士超人 自动答题:https://github.com/cxs1994/python_answer 头脑王者:https://github.com/sear ...

  4. markdown流程图语法

    从网上找了非常久关于markdown语法的文章.机会微乎其微.大多所指向的都是同一个页面https://github.com/adrai/flowchart.js 这是github上的一个开源项目,里 ...

  5. Swift开发教程--怎样设置状态栏的文字颜色

    第一步:在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO 第二步:在viewDidLoad中加一句 UIApplication.sh ...

  6. ios开发:如何用js调用ios

    本文转载至 :http://blog.chinaunix.net/uid-29415710-id-4058564.html - (BOOL)webView:(UIWebView *)webView s ...

  7. lua例子getglobal()

    #include <stdio.h> #define MAX_COLOR 255 extern "C" { #include "lua-5.2.2/src/l ...

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

  9. Hive高级

    HiveServer2 概述: https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Overview2 客户端: https:// ...

  10. C# Interactive Shell

    C# Pad 有点像VisualStudio中的ImmediateWindow,程序运行中的一些变量都保存着,可以直接从命令行访问,方便执行一些code来进行测试或debug. 上图中右边每一个小时钟 ...