AIM Tech Round 3 (Div. 2) C.Letters Cyclic Shift


/* You can just change i mean convert each character to it's previous character until you have not found 'a' to find lexicographically minimum.If you get 'a' that means it is already lexicographically minimum, then change any a character to its previous as one time. */

//#include<stdio.h>
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>
#include<bits/stdc++.h>
using namespace std;

#define fast ios_base::sync_with_stdio(false)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"

//int ar[28];

typedef long long LL;

//void ascii()
//{
//    for(int i=97; i<=122; i++)
//    {
//        ar[i] = i;
//    }
//}

int main()
{
    fast;
    //ascii();
    string s;
    while(cin >> s)
    {
        LL len = s.length() , i , tmp=0;

        bool f=false;

        for(i=0; i<len; i++)
        {
            if(s[i] != 'a')
            {
               s[i] = s[i] - 1;
                f=true;
            }

            else
            {
                if(f) break;
            }
        }

        if(!f)
        {
            if(len > 0) s[len-1] = 'z';
            else s[0] = 'z';
        }

        outn(s);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number