首页 > 其他分享 >Bear and String Distance

Bear and String Distance

时间:2024-05-15 10:19:35浏览次数:16  
标签:Distance 26 String int res Bear else input include


传送锚点:

codeforces.com

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;
}

标签:Distance,26,String,int,res,Bear,else,input,include
From: https://www.cnblogs.com/6Luffy6/p/18193287

相关文章

  • AtCoder Beginner Contest 351 E - Jump Distance Sum
    题目链接Hint1:只能斜着走,容易想到黑白棋盘,\((x+y)\%2==0\)位于一个团,\((x+y)\%2==1\)位于另一个团,分别求和。Hint2:\(dist=max(|x1-x2|,|y1-y2|)\),这是切比雪夫距离,将坐标系倾斜\(45^{\circ}\),改为曼哈顿距离\(dist=|x1-x2|+|y1-y2|\),即\(X=x+y,Y=x-y\),这样就可以将横纵坐标......
  • LeetCode 1915. Number of Wonderful Substrings
    原题链接在这里:https://leetcode.com/problems/number-of-wonderful-substrings/description/题目:A wonderful stringisastringwhere atmostone letterappearsan odd numberoftimes.Forexample, "ccjjc" and "abab" arewonderful,but "ab&......
  • 72_Edit_distance_编辑距离
    题目描述Giventwostringsword1andword2,returntheminimumnumberofoperationsrequiredtoconvertword1toword2.Youhavethefollowingthreeoperationspermittedonaword:InsertacharacterDeleteacharacterReplaceacharacter给定两个字符串wo......
  • String、StringBuffer、StringBuilder的区别
    在Java中,`String`、`StringBuffer`、和`StringBuilder`都是用于处理字符串的类,但它们之间存在一些关键差异,主要涉及可变性、线程安全性和性能:1.String:-不可变性:`String`对象一旦被创建,其内容就不能改变。任何对`String`的操作,比如拼接、替换等,都会返回一个新的`String`对象,而原......
  • String、StringBuffer、StringBuilder的区别
    在Java中,`String`、`StringBuffer`、和`StringBuilder`都是用于处理字符串的类,但它们之间存在一些关键差异,主要涉及可变性、线程安全性和性能:1.String:-不可变性:`String`对象一旦被创建,其内容就不能改变。任何对`String`的操作,比如拼接、替换等,都会返回一个新的`String`对象,而原......
  • 蓝桥杯-错误票据(两种写法stringstream和扣字符)
    某涉密单位下发了某种票据,并要在年终全部收回。每张票据有唯一的ID号。全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。你的任务是通过编程,找出断号的ID和重号的ID。假设断号不可能......
  • C++中的string
    一、定义和初始化string1、默认初始化:std::stringstr;//str是一个空字符串2、使用字面值初始化:std::stringstr1="Hello,World!";//str1包含字符串"Hello,World!"3、使用字符数组初始化:chararr[]="Hello,World!";std::stringstr3(arr);//str3包含字......
  • C#中的System.Security.SecureString学习
    有一次在公司review代码时,有一个password的字段,原来用的是String类型,有同事提到应该用SecureString比较好于是我花了点时间了解了一下什么是SecureString,以及它与String类型的区别正常的String类型值,在脱离开作用域后,它的值在内存中并不会立即被销毁.这个时候,如果有人恶意扫......
  • string:Python的文本常量与字符串模板
    前言在程序中,有很多高效率的字符串处理方式,如果开发者能够完全掌握这些高效的字符串处理,往往在开发者也能事半功倍。比如针对于字符串的处理,也是自然语言处理的基础知识。而python3中,处理字符串的库为:string。本篇将详细介绍各种字符串的高效处理方式。首字母大写对于英文单词......
  • Cannot resolve method 'and(java.util.function.Predicate<java.lang.String>)
    springboot整合knife4j报错,提示找不到该方法,用的knife4j依赖是最新版本解决方法:将knife4j版本进行降级处理,这里采用2.0.4......