Canna

Tuesday, 25 September 2018

Making of a cake

  • Problem Description

    " CodeChef has received an order from the President of Mithai country for his son's birthday cake. The president is a person of very high temper and CodeChef doesn't want to tick him him, so he had to prepare a cake exactly as described by the President's son. He asked for a cake with N layers and each layer has to be of a type specified by him. The type of layer is represented by a lowercase letter from the English alphabet.

    CodeChef asked his sous Chef, Jalebi Bai, to make this cake, who was very sleepy due to a very long and tiring journey to a planet far far away earlier.
    Due to tiredness, Jalebi Bai screwed up the the layers while baking the cake. Thankfully, it has the same number of layers as required, but any of the layers may or may not be the same as described in the order. CodeChef is really worried because of this, as making a new cake will cost him a huge amount of money.

    At this point of time, Samosa Bhai comes to the rescue. He has a layer swapper (patent pending) which can swap the layers of a cake without ruining the cake. This swapper has a limitation that it can swap layers separated exactly by distance D only, meaning there should be exactly D-1 layers in between the two layers to be swapped.

    You have to tell if the cake made by Jalebi Bai can be changed into the cake described by the President's son using Samosa Bhai's swapper. "
  • Test Case 1

    Input (stdin)
    5
    
    4 2
    
    qnyu
    
    ynqu
    
    4 1
    
    fbnc
    
    nbcf
    
    5 2
    
    abcde
    
    edacb
    
    5 2
    
    abcde
    
    edabc
    
    3 1
    
    eff
    
    bae
    
    
    Expected Output
    Yes
    
    Yes
    
    No
    
    Yes
    
    No
  • Test Case 2

    Input (stdin)
    5
    
    4 3
    
    qnyu
    
    ynqu
    
    4 1
    
    fbnc
    
    nbcf
    
    5 3
    
    abbdd
    
    edacb
    
    3 2
    
    accee
    
    edabc
    
    3 3
    
    efe
    
    bfe
    
    
    Expected Output
    No
    
    Yes
    
    No
    
    No
    
    No
  • Program
  • #include<stdio.h>
    #include <string.h>
    int main(){
      int t,n,d,flag,i,j,k;
      char a[100000],b[100000],temp;
      scanf("%d",&t);
      while (t>0) {
        scanf("%d %d",&n,&d);
        scanf("%s",a );
        scanf("%s",b );
        i = 0;
        while (i < n) {
          flag = 0;
          if (a[i] != b[i]){
            for (j = i+d;j < n;j+=d) {
              if (b[j] == a[i]){
                flag = 1;
                temp = b[j];
                for (k = j-d; k >= i; k-=d)
                  b[k+d] = b[k];
                b[k+d] = temp;
                break;
              }
            }
            if (flag == 0){
                flag = 2;
                printf("No\n");
                break;
            }
          }
          i++;
        }
        if (flag == 2){
          t--;
          continue;
        }
    
        flag = 0;
        for (i = 0;i < n;i++){
          if (a[i] != b[i]){
            flag = 1;
            break;
          }
        }
        if (flag == 1)
          printf("No\n");
        else
          printf("Yes\n");
        t--;
      }
      return 0;
    }
    

No comments:

Post a Comment

Note: only a member of this blog may post a comment.