Bear and String Distance

传送锚点:
Copy
4 26
bear
output
roar
input
2 7
af
output
db
input
3 1000
hey
output
-1
思路
此题答案不限
有点类似贪心,每一步都要做到最佳,将k不断变小
code
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int n, k;//n为单词长度,k为要求的值
cin >> n >> k;
string input;
cin >> input;
string res;//输出答案
for (int i = 0; i < input.size(); i++) {
if(!k){
//这句话不能省略,若还没遍历完input,k就为0,原封不动输出nint
res += input[i];
continue;
}
int x = input[i] - 'a', y = 'z' - input[i];
//x、y可分别理解为到字母a、z的距离
if (x >= y) {
if (x >= k) res += (char)(input[i] - k), k = 0;
//若改为input[i] + k会报错,可能会超了26个字母范围
else k -= x, res += 'a';
}
else {
if (y >= k) res += (char)(input[i] + k), k = 0;
//若改为input[i] - k会报错,可能会超了26个字母范围
else k -= y, res += 'z';
}
}
if (k) cout << -1 << endl;
else cout << res << endl;
return 0;
}
Bear and String Distance的更多相关文章
- 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 ...
- CF 628C --- Bear and String Distance --- 简单贪心
CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
- 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 ...
- String Distance and Transform Process
http://acm.hdu.edu.cn/showproblem.php?pid=1516 Problem Description String Distance is a non-negative ...
- hdu 1516 String Distance and Transform Process
Problem DescriptionString Distance is a non-negative integer that measures the distance between two ...
- GO语言的开源库
Indexes and search engines These sites provide indexes and search engines for Go packages: godoc.org ...
- 西南大学校园GIS平台
系统架构是B/S,开发语言是C#.silverlight,开发平台是.NET,数据库为sqlserver,这是我读研究生时候自己做的作品,以自己的母校为地图,进行GIS相关的功能分析,核心的模块有:空 ...
- 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离。显示为公里、米
/** * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离 * @param array $point_1 第1个点的x,y坐标 array( ...
- [简单]docx4j常用方法小结
http://53873039oycg.iteye.com/blog/2194479?utm_source=tuicool&utm_medium=referral —————————————— ...
随机推荐
- 如何通过 kubectl 进入 node shell
概述 假设这样一个场景: 生产环境中,Node 都需要通过堡垒机登录,但是 kubectl 是可以直接在个人电脑上登录的. 这种场景下,我想要通过 kubectl 登录到 K8S 集群里的 Node, ...
- 在 Visual Studio 2022 中创建一个类似于旧版本 Visual Studio 中的 Win32 Console Application 项目
在 Visual Studio 2022 中创建一个项目,其自动生成的源文件内容包含 #include "stdafx.h" 和使用_tmain 作为入口点,意味着你需要创建一个基 ...
- nginx重新整理——————热部署和日志切割[三]
前言 简单演示热部署和日志切割. 正文 什么是热部署了,我们前文也说过了一个编译后的nginx 二进制. 热部署就是无需停止现有的nginx,替换正在运行的nginx. 步骤: 复制nginx 二进制 ...
- 重学c#系列——索引器(十)
前言 对于索引器,我们肯定不会陌生.为什么会有索引器这个东西呢?我们发现这东西很像是属性,只是通过索引方式去调用,那就慢慢揭开他的面纱吧. 正文 假设我们要对外暴露一个数组,为了体现其封装性,我们使用 ...
- singalr 可以连接但是无法发送消息
前言 在一次测试singalr的过程中,发现一个问题,那就是连接成功了,但是发送消息没有响应. 原因及解决 如何可以连接但是发不了消息,net的锅,需要4.0以上,所以我们发布iis的时候也应该选择4 ...
- 百度unit闲聊机器人
import json import random import requests # client_id 为官网获取的AK, client_secret 为官网获取的SK client_id = & ...
- 自建Kubernetes集群如何使用弹性容器实例ECI
简介: 虚拟节点(Virtual Node)实现了Kubernetes与弹性容器实例ECI的无缝连接,让Kubernetes集群轻松获得极大的弹性能力,而不必受限于集群的节点计算容量.您可以灵活动态的 ...
- Kubernetes 入门教程
简介:本文是一篇 kubernetes(下文用 k8s 代替)的入门文章,将会涉及 k8s 的架构.集群搭建.一个 Redis 的例子,以及如何使用 operator-sdk 开发 operator ...
- 阿里巴巴 DevOps 工具体系
简介: 随着阿里巴巴多元化业务 20 多年的高速发展,技术体系经历了 web 时代.移动化时代.数据智能时代.云计算时代等多个重大变革.在这些变革中,开发者面对的技术体系.工具体系.知识体系也在不断 ...
- [FE] G2Plot 更新图表的两种方式
第一种是使用 G2Plot 对象上的 changeData 方法,如果不涉及到全局 title 等这些的更改,可以采用这种方式. 也就是说,只有纯数据方面的变动,使用 changeData 更新图表数 ...