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

Friday, March 28, 2014

AMSCO1 - The AMSCO cipher

Link to the question

solution of spoj AMSCO1 - The AMSCO cipher spoj

#include<stdio.h>

int main()
{
  int i,j,k,index,org_index,n,str_len,key_len,count; char str[500],key[20],result[255][255][2];

    while( scanf("%s%s",key,str) != EOF )
    {
        str_len = key_len = -1;
        while(str[++str_len] != 0);
        while(key[++key_len] != 0);

        i = -1;
        k = str_len / key_len;
        while(++i <=  k+1 )
        {
            j = -1;
            while(++j <= key_len)
                result[i][j][0] = result[i][j][1] = 0;
        }

        count = 0;
        k = str_len;
        i = org_index = -1;
        while( ++i < 255 && k)
        {
            j = -1;
            while(++j < key_len && k)
            {
                ++count;
                result[i][j][0] = str[++org_index];
                k--;

                if(count&1 && k)
                {
                    result[i][j][1] = str[++org_index];
                    k--;
                }
            }
        }

        k = key_len;
        while(k--)
        {
            char ch = 'a';

            i = -1;
            while(key[++i] != 0)
                if(key[i] < ch)
                {
                    index = i;
                    ch = key[i];
                }

            i=-1;
            while(++i<255)
            {
                if( result[i][index][0] == 0 && result[i][index][1] == 0 )
                    break;

                printf("%c",result[i][index][0]);

                if(result[i][index][1])
                    printf("%c",result[i][index][1]);
            }

            key[index] = 'a';
        }

        printf("\n");

    }

    return 0;
}