zoukankan      html  css  js  c++  java
  • 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
    standard input
    output
    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.

    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 .

    Examples
    input
    4 26
    bear
    output
    roar
    input
    2 7
    af
    output
    db
    input
    3 1000
    hey
    output
    -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"<<"
    ";
        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;
    }
  • 相关阅读:
    STL应用 map poj 2418
    STL应用 set hdu 1412
    STL应用 deque hdu 6375
    STL应用 queue poj 1915
    STL应用 map HDU 3527
    算法训练营 入门篇 STL应用 vector HDU 3527
    算法训练营:海量图解+竞赛刷题(入门篇)刷题, 算法基础知识点讲解与练习
    BFS 遍历例子
    【知识】MySQL索引原理及慢查询优化
    【MySQL优化】——看懂explain
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5203057.html
Copyright © 2011-2022 走看看