628C - Bear and String Distance - CodeForces Solution


greedy strings *1300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<< #x <<" : "<<x<<endl;
#define dbgv(v) cout<< #v <<" : ";for(auto &i:v) cout<<i<<" ";cout<<endl;
#define endl '\n'
const int MOD = 1e9+ 7;

int main() {
	ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
	int T = 1,i;
	//cin>>T;
	while(T--) {
    	//string S;
    	//cin>>S;
    	int N,K;
    	cin>>N>>K;
    	string S;
    	cin>>S;
    	string V="";
    	for(i=0;i<N;i++)
    	{
    		if(K>0)
    		{
    			int P=max(abs(S[i]-'a'),abs(S[i]-'z'));
    			if(P<K)
    			{
    				if(P==abs(S[i]-'a'))
    				{
    					S[i]='a';
    				}
    				else
    				{
    					S[i]='z';
    				}
    				K=K-P;
    			}
    			else if(P==K)
    			{
    				if(P==abs(S[i]-'a'))
    				{
    					S[i]='a';
    				}
    				else
    				{
    					S[i]='z';
    				}
    				K=0;
    			}
    			else
    			{
    				if(S[i]+K<='z')
    				{
    					S[i]=S[i]+K;
    					K=0;
    				}
    				else
    				{
    					S[i]=S[i]-K;
    					K=0;
    				}
    			}
    		}
    		if(K==0)
    		{
    			break;
    		}
    	}
    	//cout<<K<<endl;
    	if(K==0)
    	{
    		cout<<S<<endl;
    	}
    	else
    	{
    		cout<<-1<<endl;
    	}
	}
}


Comments

Submit
0 Comments
More Questions

838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices