Sunday, August 8, 2010

Caesar Cypher



You have intercepted the data transmission of the
enemy. It is known that the encryption being used is Caesar Cypher. In this
encryption technique, if the letter to be encrypted is the Nth letter in the alphabet,
replace it with the (N-K)th
where K is some fixed integer called the Key. This addition is done modulo 26.
The spaces in the message are not encrypted.



Input Specification



There will be a number of test cases. Each test case consists of an encrypted
message of maximum 200 characters and containing only uppercase letters and
spaces, and an integer k (1<=k<=1000) which is the key of the encryption.
End of file denotes the end of input.



Output Specification



Your output should be the decrypted message, each in a line by itself.



Example Input



LJMM PCBNB
1
LJMM PCBNB
53


Example Output



KILL OBAMA
KILL OBAMA
#include<iostream>
#include<string>
using namespace std;
int main()
{
          char A[300];
          int key,i=0,j=0;
          char d;
          while(gets(A))
          {
                    key=0;i=0;j=0;
                    cin>>key;
                    //Since the counting is cyclic therefore counting multiples of 26 would give the original alphabet.
                    key=key%26;
                    for(i=0;i<strlen(A);i++)
                   {
                             if(A[i]!=' ')
                            {
                                       for(j=0;j<key;j++)
                                      {
                                           //Condition for the encryption to count from Z once A is encountered.
                                                if(A[i]=='A')
                                               {
                                                          A[i]='Z';
                                                          continue;
                                               }
                                               A[i]=A[i]-1;
                                     }
                           }                       
                  }
                  cout<<A<<endl;
                  d=getchar();
         }
         return 0;
}

Saturday, August 7, 2010

Longest Paths

Your goal is to develop a program that
computes the length of the longest path that can be constructed in a
given
graph from a given starting point. You can assume
that
the graph has no cycles (there is no path from any node to itself). In the same line of
reasoning,
nodes are not considered directly connected to themselves.




Input
 


The input
consists of a number of cases. The first line on each case contains a
positive
number n (

)
that specifies the number of nodes in the graph. A value of n = 0
indicates the end of the input.




After this, a second number s is provided,
indicating the starting point (

). Then,
you
are given a list of pairs of places p and q, one pair per
line, with the
places on each line separated by white-space. The pair ``" indicates
that q can be visited after p. A pair of zeros (``0 0") indicates
the end of the case.





Output
 


For each test case you have to find the length of the
longest path that begins at the starting place. You also have to print
the
number of the final place of such longest path. If there are several
paths of
maximum length, print the final place with smallest number.
Print a new line after each test case.




Sample Input
 


2
1
1 2
0 0
5
3
1 2
3 5
3 1
2 4
4 5
0 0
5
5
5 1
5 2
5 3
5 4
4 1
4 2
0 0
0





Sample Output
 


Case 1: The longest path from 1 has length 1, finishing at 2.

Case 2: The longest path from 3 has length 4, finishing at 5.

Case 3: The longest path from 5 has length 2, finishing at 1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
class Program
{
static int maxcount = 0;//Variable to store the length of the longest path.
static int final = 0;//Variable to store the final node.
static bool visit = false;//Variable to check if the else part has been visited at least once.
static void Main(string[] args)
{
int num = int.Parse(Console.ReadLine());
int start = int.Parse(Console.ReadLine());
int initial = start;
int[,] arr = new int[num, num];
int[] trav = new int[num];
int num1, num2;
//Converting the input into the corresponding incidence matrix.
while((num1= int.Parse(Console.ReadLine()))!=0)
{
num2 = int.Parse(Console.ReadLine());
arr[num1 - 1, num2 - 1] = 1;
}
pathfinder(arr, start - 1, 0, trav);
Console.WriteLine("The longest path from " + initial + " has length " + maxcount + " finishing at " + final);
}
static void pathfinder(int[,] arr, int row, int count, int[]trav)
{
//Base case: The node from which no link to other nodes is found.
if (trav[row] == 1 && arr[row, arr.GetLength(0) - 1] == 0 && visit == true)
{
if (maxcount < count)
{
maxcount = count;
final = row + 1;
}
return;
}
else
{
visit = true;
for (int i = 0; i < arr.GetLength(0); i++)
{
trav[row] = 1;
if (arr[row, i] == 1)
{
count++;
//Sending the control to that row, the link of which is found here.
pathfinder(arr, i, count, trav);
trav[row] = 0;
count--;
}
}
}
if (maxcount < count)
{
maxcount = count;
final = row + 1;
}
}
}
}





Thursday, August 5, 2010

Maximum number of rings



There are a number of rings lying on the floor.
Any two rings can intersect at two points, may not intersect at all, or touch
each other at a single point. Two rings can be lifted simultaneously if and only
if they intersect at two points. A bunch of rings mutually satisfying this
condition can be lifted at once. You have to find the maximum number of rings
that can be lifted in a single attempt. The rings are specified by their x and y coordinates on the floor and their radius, the specifications of a ring being in a single row of a matrix.



        static void Main(string[]
args)



        {          



            int
maxcount = 1;



            double[,]
arr ={



                               {0.0, 0.0, 1.0},



                               {1.0, 0.0, 1.0},



                               {2.0, 0.0, 1.0},



                               {-1.0, 0.0, 1.0}



                           };



            int
num = arr.GetLength(0);



            //Array
to keep track of the rings included in the present bunch.



            int[]
used = new int[num];



            for
(int i = 0; i < arr.GetLength(0); i++)//Denotes the starting ring.



            {



                int
thiscount = 1;



                for
(int m = 0; m < num; m++)



                {



                    used[m] = 0;



                }



                used[i] = 1;//Starting ring has been used.



                //Searching
a ring from the beginning which matches the conditions.



                for
(int j = 0; j < arr.GetLength(0); j++)



                {                                               



                    if
(used[j] == 0)



                    {



                        //Searching from the beginning from the beginning of the current bunch.



                        for (int n = 0; n < num; n++)



                        {



                            double dist = Math.Sqrt(Math.Pow((arr[n, 0] - arr[j, 0]), 2.0d) + Math.Pow((arr[n, 1] - arr[j, 1]), 2.0d));



                            if (used[n] == 1 && dist < arr[j, 2] +
arr[n, 2] && dist > Math.Abs(arr[j,
2] - arr[n, 2]))



                            {



                                used[j] = 1;



                                thiscount++;



                                break;



                            }



                        }



                    }



                }



                if
(thiscount > maxcount)



                {



                    maxcount = thiscount;



                }



            }



            Console.WriteLine("The maximum number of rings is: " +
maxcount);



        }

Sunday, August 1, 2010

Higher n higher



Kapil is climbing the steps of his
office after meeting Anshuman. The length of a step must be nonnegative and can
be by one bigger than, equal to, or one smaller than the length of the previous
step. Since his time is very precious he needs to do this using the minimum
number of steps in order to get from x to y. He needs your help for designing
an algorithm for this feat. The length of the first and the last step must be
one.



What is the minimum number of steps
in order to get from x to y? The length of the first and the last
step must be 1.



Input and Output 



Input consists of a line containing
n, the number of test cases. For each test case, a line follows with two
integers: 0 < x < y < 2^31. For each test case, print a line giving
the minimum number of steps to get from x to y.



Sample Input 





3

17
41



1
41



14604
32391



 



Sample Output 



9



12



266



#include<iostream>



using
namespace std;



int main()



{



    long int test,num1,num2,incr,decr,count;



                cin>>test;



                while(test>0)



                {



                                count=0;



                                incr=1;decr=1;



                                cin>>num1;



                                cin>>num2;



                                while(num1<num2)



                                {



                                                num1=num1+incr;



                                                incr++;



                                                count++;



                                                if(num1<num2)



                                                {



                                                                num2=num2-decr;



                                                                decr++;



                                                                count++;



                                                }



                                }



                                cout<<count<<endl;



                                test--;



                }



                return
0;



}

Cutting Blocks








Niloy is a cool mathematician and
is good at solving puzzles.He has been given
a puzzle
which he solves very quickly but he wants to check his solution. Now
your task
is to find the answer so that Niloy can match his answer.
Here is the puzzle :)
Given a block of W weight you have to cut the
block
into blocks having a weight of K or less. Each block can be a cut only
if its
weight is greator than the minimum weight k,
such
that it results in blocks with minimal weight difference. We can further
cut
the newer blocks but we can apply only one cut to a particular block.
The cuts
are made such that we get blocks of integral weight. K and w
are both integer.



Input 



n - no of cases
w
( integer ) - weight of original block
K  (
integer ) -  maximum weight of final blocks to be formed
2 < = W  <= 10000
1 < = K


Output 



no of
final blocks formed



Sample input 



5
16 1
4 2
16 2
32 4
11 2


Sample Output 



16
2
8
8
7

#include<iostream>
using namespace std;
int main()
{
int fun(int a,int b);
int test,num1,num2,count;
cin>>test;
     while(test>0)
{
cin>>num1;
cin>>num2;
          count=fun(num1,num2);
      
   cout<<count<<endl;
      
   test--;
    
}
return 0;
}
int fun(int a,int b)
{
if(a<=b)
     {return 1;}
    
else
     {
   
      return fun(a/2,b)+fun(a-(a/2),b);
    }
}

Saturday, July 17, 2010

Power Set



Printing all the
possible combinations of a set of elements



using System;



using
System.Collections;



using
System.Linq;



using
System.Text;



 



namespace
ConsoleApplication3



{



    class Program



    {



        static int count = 0;//Variable
which counts the number of combinations printed.



        static void Main(string[]
args)



        {



            ArrayList
list = new ArrayList();



            string
input = "abcde";



            char[]
arr = new char[input.Length];



            arr = input.ToCharArray();



            //int[]
arr = { 1, 2, 3, 4, 5 };



            combine(arr,list,0);



            Console.WriteLine("The total number of combinations is: "
+ count);



        }



        //Change
char[]arr to int[]arr to use the function for an int array.



        static void combine(char[]
arr, ArrayList list, int index)



        {



            if
(index == arr.Length)



            {



                return;



            }



            else



            {



                while
(index < arr.Length)



                {



                    list.Add(arr[index]);



                    //Printing
format



                    Console.Write("{");



                    for
(int i = 0; i < list.Count; i++)



                    {



                        if (i !=
list.Count - 1)



                        {



                            Console.Write(list[i] + ",");



                        }



                        else



                        {



                            Console.Write(list[i]);



                        }



                    }



                    Console.Write("}");



                    Console.WriteLine();



                    //Printing
format.



                    count++;



                    combine(arr, list, index +
1);



                    //Removing
the last element of the present list on returning.



                    list.RemoveAt(list.Count -
1);



                    index++;



                }



            }



        }



    }



}



Program to solve N-queens problem by back-tracking

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace
ConsoleApplication4
{
    class Program
    {
        static int total = 0;//Variable which counts the number of possible arrangements.
        static void Main(string[] args)
        {
            int num = 8;
            int[,] board = new int[num,num];
            for(int i = 0; i < num; i++)
            {
                for(int j = 0; j < num; j++)
                {
                    board[i, j] = 0;
                }
            }
            queens(board, num, 0, 0, 0);
            Console.WriteLine("The number of possible arrangements is: " + total);
        }

        static void queens(int[,] board, int num, int count, int row, int j)
        {
            if(count == num)
            {
                total++;
                return;
            }
            if(j >= num && row >= num)
            {
                return;
            }
            else
            {
                j = 0;
                while(j < num && row < num)
                {
                    if(board[row, j] == 0)
                    {
                        int[,] prev = new int[num, num];
                        for (int m = 0; m < board.GetLength(0); m++)
                        {
                            for (int n = 0; n < board.GetLength(1); n++)
                            {
                                prev[m, n] = board[m, n];
                            }
                        }
                        board[row, j] = 1;
                        count++;
                        //Function to mark the squares where a queen cannot be placed.
                        queenplace(board, row, j);
                        queens(board, num, count, row + 1, j);

                        //Copying the board conditions of the previous call on returning.
                        for (int m = 0; m < num; m++)
                        {
                            for (int n = 0; n < num; n++)
                            {
                                board[m, n] = prev[m, n];
                            }
                        }
                        count--;
                        j++;
                    }
                    else
                    {
                        j++;
                    }
                }
            }
        }
        static void queenplace(int[,] board, int row, int j)
        {
            //Marking all squares of the row.
            for(int i = 0; i < board.GetLength(1); i++)
            {
                board[row, i] = 1;
            }
            //Marking all squares of the column.
            for(int i = 0; i < board.GetLength(0); i++)
            {
                board[i, j] = 1;
            }
            int rowpoint=row;
            int colpoint=j;
           //Marking the diagonal downwards towards the left.
            while(colpoint >= 0 && rowpoint < board.GetLength(0))
            {
                board[rowpoint, colpoint] = 1;
                rowpoint++;
                colpoint--;
            }
            rowpoint = row;
            colpoint = j;
            //Marking the diagonal downwards towards the right.
            while(colpoint < board.GetLength(1) && rowpoint < board.GetLength(0))
            {
                board[rowpoint, colpoint] = 1;
                rowpoint++;
                colpoint++;
            }
        }
    }
}