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

AMSCO2 - Decipher the AMSCO cipher

Link to the question

solution to spoj AMSCO2 - Decipher the AMSCO cipher
Click Here, for more details on AMSCO cipher but you won't need it.
#include<stdio.h>

int main()
{
    int i,j,t,k,len_str,len_key,count,index,enc_index;  char ch,key[100],str[300];

    while(scanf("%s%s",key,str) != EOF)
    {
        int count = 0;    char hold[255][255] = {0},result[255][255][2];

        len_str = -1;
        while(str[++len_str] != 0);
        len_key = -1;
        while(key[++len_key] != 0);

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

        i=-1;
        k = len_str;
        while(++i < 255 && k)
        {
            j=-1;
            while(++j < len_key && k)
            {
                ++count;

                if(count&1 && k >= 2)
                {
                    hold[i][j] = '2';
                    k -= 2;
                }
                else
                {
                    hold[i][j] = '1';
                    k--;
                }
            }
        }

        enc_index = -1;
        k = len_key;
        while(k--)
        {
            ch = 'a';

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

            i = -1;
            while(hold[++i][index])
            {
                result[i][index][0] = str[++enc_index];

                if(hold[i][index] == '2')
                {
                    result[i][index][1] = str[++enc_index];
                }
            }

            key[index] = 'a';
        }

        i = -1;
        k = len_str;

        while(++i <255 && k)
        {
            j = -1;
            while(++j < len_key && k)
            {
                printf("%c",result[i][j][0]);
                k--;
                if(result[i][j][1])
                {
                    printf("%c",result[i][j][1]);
                    k--;
                }
            }
        }

        printf("\n");
    }

    return 0;
}