codeforces 628C 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
题意:
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+4;
char s[N];
int n,k;
int main()
{
int num=0;
scanf("%d%d",&n,&k);
scanf("%s",s);
for(int i=0;i<n;i++)
{
num+=max(s[i]-'a','z'-s[i]);
}
if(k>num)cout<<"-1"<<"\n";
else
{
int sum;
for(int i=0;i<n;i++)
{
sum=max(s[i]-'a','z'-s[i]);
//cout<<sum<<endl;
if(sum<=k){
k-=sum;
if(s[i]-'a'>'z'-s[i])s[i]='a';
else s[i]='z';}
else if(sum>k&&k!=0)
{
if(s[i]-'a'>=k)
{
s[i]=s[i]-k;
}
else
{
s[i]=s[i]+k;
}
k=0;
}
printf("%c",s[i]);
} }
return 0;
}
codeforces 628C C. Bear and String Distance的更多相关文章
- 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 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 ...
- String Distance and Transform Process
http://acm.hdu.edu.cn/showproblem.php?pid=1516 Problem Description String Distance is a non-negative ...
- codeforces 680C C. Bear and Prime 100(数论)
题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
- codeforces 653B B. Bear and Compressing(dfs)
题目链接: B. Bear and Compressing time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)
题目链接: C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
随机推荐
- 转:MSSQL SERVER行转列 列转行
SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PI ...
- 从动态获取div高度的问题展开来看
ps 可能篇幅比较长,请大家耐心看看 今天有人在群里问我 动态获取高度怎么获取 我就说jq中的outerHeight. height .innerHeight 原生的height clientH ...
- shader随记
o.WorldPos = normalize(mul((float4x4)unity_ObjectToWorld, v.vertex)).xyz;
- iOS 平台如何使用 TestFlight 进行 Beta 测试
使用 TestFlight,你可以向测试人员发布你 App 的 prerelease 版本来收集反馈信息,为将来发布 App 的正式版做准备.现在 TestFlight 是一个可选功能,你也可以不使用 ...
- Android自定义属性format类型
1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> <attr name = &quo ...
- 装服务器,测试数据库,简单的maven命令
[说明]今天总体回顾一下:大概是早上装服务器,下午测试数据库,晚上了解简单的maven命令 一:今日完成 1)在远程服务器的tomcat 设置好管理员的登录账号 2)登录tomcat 的项目管理 查看 ...
- lua 元表是个啥?
function readOnly(t) local proxy = {} local mt = { __index = t, __newindex = function(t,k,v) error(& ...
- 第三课 nodejs读取文件
//引入文件操作模块var fs = require('fs'); //读取文件 使用 回调函数 utf-8编码读取 a.txt在当前文件目录fs.readFile('a.txt','UTF-8',f ...
- HTML元素嵌套关系
- debian安装oracle jdk
1 去官网下载linux jdk https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...