Thursday, September 13, 2018

LETTERS CYCLIC SHIFT


  • Problem Description

    "You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z' 'y' 'x' 'b' 'a' 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'.
    What is the lexicographically minimum string that can be obtained from s by performing this shift exactly once?

    Input
    The only line of the input contains the string s (1<|s|<100000) consisting of lowercase English letters.

    Output
    Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.

    Note
    String s is lexicographically smaller than some other string t of the same length if there exists some 1i|s|, such that s1=t1,s2=t2,...,si-1=ti-1, and si<ti."
  • CODING ARENA
  • #include <stdio.h>
    #include <string.h>
    int main()
    {
      char s[100001];
      int i,k=0;
      scanf("%s",s);
      //l=strlen(a);
      for(i=0;s[i]!='\0';i++)
       if(s[i]!='a')
        break;
      for(;s[i]!='\0';i++) 
      {
        if(s[i]=='a')
          break;
        s[i]--;
        k++;
      }
      if(!k)
        s[strlen(s)-1]='z';
      puts(s);
      return 0;
    }

    /*
    int main()
    {
      char *shiftstr(char str[],int offset)
      {
        unsigned int i;
        for(i=0;str!='\0';i++)
        {
          str[i]+=offset;
        }
        return str;
      }
    return 0;
    }*/
  • Test Case 1

    Input (stdin)
    codeforces
    
    
    Expected Output
    bncdenqbdr
  • Test Case 2

    Input (stdin)
    srmulc
    
    
    Expected Output
    rqltkb

1 comment: