【CodeForces 628C 】Bear and String Distance (贪心)

本文介绍了一种通过贪心算法解决字符串匹配问题的方法,给定一个字符串和一个整数k,寻找与原字符串各字符间ASCII值之差总和等于k的新字符串。

Description

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 use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Input

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.

Output

If there is no string satisfying the given conditions then print “-1” (without the quotes).

Otherwise, print any nice string s’ that .

Sample Input

Input

4 26
bear

Output

roar

Input

2 7
af

Output

db

Input

3 1000
hey

Output

-1

题目大意

约定两个字母之间的差为两者ASCII值的差的绝对值,给定一串英文字符的字母个数n以及规定要达到的差值d,输入一个字符串s,求与这个字符串差为d的字符串

思路

用贪心从第一位开始,与’z’和’a’比较,如果用’z’或者’a’作为这一位的答案之后还是无法达到差值k,那么如果这一位与’z’的差值大就用’z’作为这一位的答案,反之用’a’,总之就是要是k减去这一位的差值要尽可能的小,然后后移一位继续贪心。如果到某一位(这一位必须小于等于n)后不满足上述条件,那么这是当前串的差值与输入的字符串的差值一定小于’z’-s[i]>=k或者s[i]-‘a’>=k,那么如果是第一种情况就用s[i]+k作为这一位的答案,反之用s[i]-k作为这一位的答案。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=1e5+5;

int n,k;
char str[maxn];
char ans[maxn];

int main()
{
    while(~scanf("%d %d",&n,&k))
    {
        scanf("%s",&str);
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            if(str[i]+k>'z'&&str[i]-k<'a')
            {
                if('z'-str[i]>=str[i]-'a')
                {
                    ans[i]='z';
                    k-='z'-str[i];
                }
                else
                {
                    ans[i]='a';
                    k-=str[i]-'a';
                }
            }
            else if(str[i]+k<='z')
            {
                ans[i]=str[i]+k;
                k=0;
            }
            else if(str[i]-k>='a')
            {
                ans[i]=str[i]-k;
                k=0;
            }

        }
        if(!k) printf("%s\n",ans);
        else printf("-1\n");
    }
    return 0;
}
### 解决方案概述 对于给定的问题,在一维坐标系中有 N 个点,每个点具有特定的权重 w。如果两个点之间的距离大于它们各自权重之和,则这两个点之间可以建立一条边。目标是从这些点中找到最大数量的点使得任意两点间均存在边。 为了实现这一目的,可以通过构建不等式 Xi-Wi ≥ Xj+Wj 来确定有向图中的节点关系[^3]。具体来说: - 对于每一对不同的点 (Xi, Wi)(Xj, Wj),当 Xi > Xj 并且 Xi - Wi >= Xj + Wj 成立时,表示从 j 到 i 存在一个方向。 - Xj| >= Wi + Wj 的判断过程。 基于上述分析,算法设计如下: 1. 创建一个新的数组 P[],其中存储的是经过变换后的数据对 (Xi + Wi, Xi - Wi)。 2. 将此新创建的数据集按照第二个分量升序排列;如果有相同的情况,则按第一个分量降序处理。 3. 初始化计数器 count=0 及当前最小右端点 cur_min_right=-∞。 4. 遍历排序后的列表: - 如果当前元素的第一个分量大于等于cur_min_right,则更新count并设置新的cur_min_right为该元素的第二分量。 5. 输出最终的结果即为所求的最大团大小。 以下是 Python 实现代码示例: ```python def max_clique(n, points): # 计算转换后的点集合 [(xi+wi, xi-wi)] transformed_points = sorted([(points[i][0]+points[i][1], points[i][0]-points[i][1]) for i in range(n)], key=lambda x:(x[1], -x[0])) result = 0 current_end = float('-inf') for start, end in transformed_points: if start >= current_end: result += 1 current_end = end return result ``` 通过这种方法能够有效地解决问题,并获得最优解。值得注意的是,这里采用了一种贪心策略来逐步增加符合条件的顶点数目,从而保证了结果是最优的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值