Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Monday, February 10, 2014

CIPHERJ-Cipher

Cipher

below given code is for cipherj spoj or cipher spoj.
In this problem you have to find cycle of repetition .
lets take given example.
10
4 5 3 7 2 8 1 6 10 9
1 Hello Bob
1995 CERC
0
0

for given array of ten length repetition are as follows.
___|0__1__2__3__4__5__6__7__8__9__10_
   |
 0 |0  1  2  3  4  5  6  7  8  9  10
 1 |0  4  5     7  2  8  1  6  10  9
 3 |0  7        1        4          
word in first position will repeat on 1st 4th and 7th position so if given k=1995
then word at first position will go-to pos=1995%3 which is 0 so number at index [0,1]=1 character at position 1 is C;
and so on for all number;

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;
int main()
{
 int n;
 while(1)
 {
  scanf("%d",&n);
  if(n==0)
   break;
  int a[n+5];
  int i,j,k,count=1;
  int res[210][210];
  for(i=1;i<=n;i++)
  {
   scanf("%d",&a[i]);
   res[0][i]=i;
  }
  while(1)
  {
   scanf("%d",&k);
   if(k==0)
    break;
   char *word;
   word=(char *)calloc(210,sizeof(char));
   char *final=(char *)calloc(210,sizeof(char));
   fflush(stdin);
   gets(word);
   int count,pos;
   for(i=1;i<=n;i++)
   {
    count=1;
    j=i;
    while(a[j]!=i)
    {
     res[count++][i]=a[j];
     j=a[j];
    }
    pos=res[k%count][i];
    final[pos-1]=word[i]==0?32:word[i];
   }
   for(j=0;j<n;j++)
    printf("%c",final[j]);
    printf("\n");
  }
 }
 return 0;
}

No comments:

Post a Comment

Your comment is valuable to us