codeforces_628C. Bear and String Distance

来源:互联网 发布:情侣手环淘宝 互相感应 编辑:程序博客网 时间:2024/06/05 17:20
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 ≤ 1050 ≤ 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 26bear
output
roar
input
2 7af
output
db
input
3 1000hey
outp
ut
-1
这道题没有难度,不过我却一直看不懂题意,一直以为是输出所有的满足题意的字符串,后来才明白是输出任意一个。。醉了,吸取经验吧。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;char a[100010],b[100010];int main(){    int n,k,i;    scanf("%d%d",&n,&k);    int kk=k;    scanf("%s",&a);    for(i=0;i<n;i++)    {        if(k==0)b[i]=a[i];        else if(a[i]+k>'z'&&a[i]-k<'a')        {            if('z'-a[i]>a[i]-'a')            {                b[i]='z';                k-=('z'-a[i]);            }            else            {                b[i]='a';                k-=(a[i]-'a');            }        }        else if(a[i]+k<='z')        {            b[i]=a[i]+k;            k=0;        }        else if(a[i]-k>='a')        {            b[i]=a[i]-k;            k=0;        }    }    if(k!=0){printf("-1");return 0;}    b[i]='\0';    puts(b);    return 0;}



0 0
原创粉丝点击