您的位置:首页 > 其它

Codeforces CF#628 Education 8 C. Bear and String Distance

2016-12-02 12:07 615 查看
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,

1 #include <cstdio>
2 #include <cstring>
3 #include <string>
4 #include <iostream>
5 #include <algorithm>
6 using namespace std;
7
8 const int N = 100010;
9 int n, k;
10 char str
;
11 char ans
;
12
13 int main() {
14     scanf("%d%d", &n, &k);
15     scanf("%s", str);
16     memset(ans, 0, sizeof(ans));
17     for(int i = 0; i < n; ++i) {
18         int dis = min(k, max(str[i] - 'a', 'z' - str[i]));
19         if(str[i] - 'a' >= dis) ans[i] = str[i] - dis;
20         else ans[i] = str[i] + dis;
21         k -= dis;
22     }
23     if(k) puts("-1");
24     else printf("%s\n", ans);
25     return 0;
26 }


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: