464A - No to Palindromes - CodeForces Solution


greedy strings *1700

Please click on ads to support us..

C++ Code:

#include<iostream>
#include <vector>
#include <map>
#include <numeric>
#include<algorithm>
#include<set>
using namespace std;

typedef long long int ll;
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0)

int main()
{
    fast;
    ll n, k;
    cin>>n>>k;
	string s;
	cin>>s;
	bool ans=false;
	int ind=-1;
	for(int i=n-1;i>=0;i--)
	{
	    int p=s[i]-'a'+1;
	    for(int j=p;j<k;j++)
	    {
	        if ((i >= 1 && j == s[i - 1] - 'a') || (i >= 2 && j == s[i - 2] - 'a'))
                continue;
            s[i] = char(j + 'a');
            ind = i + 1;
            ans = 1;
            break;
	    }
	    if(ans)
	    break;
	}
	if(!ans)
	{
	    cout<<"NO"<<endl;
	    return 0;
	}
	for (int i = ind; i < n; i++)
    {
        for (int j = 0; j < k; j++)
        {
            if ((i >= 1 && j == s[i - 1] - 'a') || (i >= 2 && j == s[i - 2] - 'a'))
                continue;
            s[i] = char(j + 'a');
            break;
        }
    }
	for(int i=0;i<n;i++)
	{
	    cout<<s[i];
	}
	cout<<endl;
}


Comments

Submit
0 Comments
More Questions

810B - Summer sell-off
84A - Toy Army
185A - Plant
1749A - Cowardly Rooks
1749C - Number Game
1749B - Death's Blessing
1749D - Counting Arrays
1447B - Numbers Box
1594D - The Number of Imposters
984B - Minesweeper
837A - Text Volume
1566C - MAX-MEX Cut
1546A - AquaMoon and Two Arrays
897B - Chtholly's request
1363B - Subsequence Hate
437B - The Child and Set
1256B - Minimize the Permutation
733B - Parade
172A - Phone Code
148D - Bag of mice
421A - Pasha and Hamsters
1393A - Rainbow Dash Fluttershy and Chess Coloring
980E - The Number Games
219B - Special Offer Super Price 999 Bourles
560B - Gerald is into Art
322B - Ciel and Flowers
801B - Valued Keys
975C - Valhalla Siege
518B - Tanya and Postcard
514B - Han Solo and Lazer Gun