Codeforces CF#628 Education 8 C. Bear and String Distance
1 second
256 megabytes
standard input
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.
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.
If there is no string satisfying the given conditions then print "-1" (without the quotes).
Otherwise, print any nice string s' that
.
4 26
bear
roar
2 7
af
db
3 1000
hey
-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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
- CF 628C --- Bear and String Distance --- 简单贪心
CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...
- 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 ...
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
随机推荐
- 使用angular.bootstrap() 完成模块的手动加载
之前我们看到使用ng-app指令,可以实现模块的自动加载.现在我们看下,angular中如何手动加载模块.需要使用到angular.bootstrap这个函数. <html> <he ...
- C#在二维码中添加圆角logo
public class QRCodeHelper { #region 合并用户QR图片和用户头像 /// <summary> /// 合并用户QR图片和用户头像 /// </sum ...
- SQLite.Net-PCLUSING SQLITE IN WINDOWS 10 UNIVERSAL APPS
USING SQLITE IN WINDOWS 10 UNIVERSAL APPS 1.下载SQLite VSIX package并安装 http://sqlite.org/download.html ...
- 使用vue1.0+es6+vue-cli+webpack+iview-ui+jQuery 撸一套高质量的后台管理系统
首先按照vue.js官网的指令安装: 1.本地安装好node.js 2.根据官方命令行工具 详情 这样一个官方的脚手架工具就已经搭建好了:但是有一点需要注意的是由于现在按照官方的搭建方法是搭建vue2 ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- javascript中的对象
除了字符串,数字,布尔值(true,false),null,undefined,js中的值都是对象. 操作一个对象 var o = {name: 'man', value: 99} o.name = ...
- 安天AVL联合小米MIUI首擒顽固病毒“不死鸟”
不死鸟作为希腊神话中的一种怪物,拥有不断再生的能力,每当寿限将至时,它会在巢穴中自焚,并在三天后重新复活.就在近期,安天AVL移动安全团队和小米MIUI安全中心发现了病毒界的“不死鸟”,其顽固程度之深 ...
- addEventListener详解
为什么需要addEventListener? 先来看一个片段: html代码 <div id="box">追梦子</div> 用on的代码 window.o ...
- oracle对/dev/shm的使用
查看共享内存打开的文件数 [root@db2 ~]# lsof -n | grep /dev/shm | wc -l 34693 共享内存中总共文件数 [root@db2 ~]# ls -l /dev ...
- Denormalization
Denormalization In computing, denormalization is the process of attempting to optimize the read perf ...