Tuesday, July 17, 2012

Simple Minded Hashing

All of you know a bit or two about hashing. It involves mapping an element into a numerical value using some mathematical function. In this problem we will consider a very ‘simple minded hashing’. It involves assigning numerical value to the alphabets and summing these values of the characters.

For example, the string “acm” is mapped to 1 + 3 + 13 = 17. Unfortunately, this method does not give one-to-one mapping. The string “adl” also maps to 17 (1 + 4 + 12). This is called collision.

In this problem you will have to find the number of strings of length L, which maps to an integer S, using the above hash function. You have to consider strings that have only lowercase letters in strictly ascending order.

Suppose L = 3 and S = 10, there are 4 such strings.

  1. abg

  2. acf

  3. ade

  4. bce


agb also produces 10 but the letters are not strictly in ascending order.

bh also produces 10 but it has 2 letters.

Input

There will be several cases. Each case consists of 2 integers L and S(0 < L, S < 10000). Input is terminated with 2 zeros.

Output

For each case, output Case #: where # is replaced by case number. Then output the result. Follow the sample for exact format. The result will fit in 32 bit signed integers.











Sample InputOutput for Sample Input
3 10
2 3
0 0
Case 1: 4
Case 2: 1


#include<stdio.h>
int main()
{
int fun(int,int,int,int,int,int,int[]);
int t,inp,num,A[10000]={0};
scanf("%d %d",&inp,&num);
if(inp==0 && num==0)
{
return 0;
}
else
{
printf("Count= %d\n",fun(1,inp,0,0,num,0,A));
}
return 0;
}
int fun(int level,int inp,int sum,int curr,int num,int count,int A[10000])
{
if(level==inp)
{
if(num-sum<=26 && num-sum>curr)
{
for(int i=0;i<level-1;i++)
{
printf("%d ",A[i]);
}
printf("%d\n",num-sum);
count+=1;
return count;
}
else
{
return count;
}
}
else
{
for(int i=curr+1;i<=(num/(inp-1));i++)
{
sum+=i;
A[level-1]=i;
count=fun(level+1,inp,sum,i,num,count,A);
sum-=i;
}
return count;
}
}

Saturday, July 7, 2012

Determinant Calculator

C-program to calculate the determinant of a square matrix of any dimensions up to [100x100]

#include<stdio.h>
int main()
{
int determinant(int A[][100],int n,int i,int s);
int A[100][100],i,n,B[100];
printf("Enter the number of rows or columns: \n");
scanf("%d",&n);
printf("Enter the square matrix: \n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("The determinant is: \n");
int s=0;
s=determinant(A,n,i,s);
printf("%d\n",s);
fflush(stdin);
getchar();
return 0;
}
int determinant(int A[][100],int n,int i,int s)
{
int B[100][100];
int a=s;
if(n==2)
{
return A[0][0]*A[1][1] - A[0][1]*A[1][0];
}
else
{
//int s=0;
for(int j=0;j<n;j++)
{
int x=0;
for(int p=1;p<n;p++)
{
int y=0;
for(int q=0;q<n;q++)
{
if(q!=j)
{
B[x][y]=A[p][q];
y++;
}
}
x++;
}
if(j%2==0)
{
s+=A[0][j]*determinant(B,n-1,j,a);
}
else
{
s-=A[0][j]*determinant(B,n-1,j,a);
}
}
return s;
}
}
Output:
Enter the number of rows or columns: 4
Enter the square matrix:
1 13 4 6
23 21 18 5
4 2 7 9
1 10 15 12
The determinant is:
20353

Monday, April 16, 2012

Merging non-overlapping intervals

Problem:


Given a set of non overlapping intervals
Example 1: (1,4) (6,10) (14, 19) and another interval (13, 17) merge them as (1,4) (6,10) (13,19)


Example 2: (1,5) (6, 15) (20, 21) (23, 26) (27, 30) (35, 40)
New interval (14, 33)
Output should be
(1,5) (6, 33) (35, 40)


This is because the new interval overlaps with (6, 15) (20, 21) (23, 26) (27, 30)


Input:


The first line contains a number T, the number of test cases.


The next line contains an integer N, which is the number of non-overlapping intervals.


The next N lines contain two space separated integers which denote the start and end points of an interval. This is followed by a blank line.


The next line contains two space separated integers which denote the new interval.


Output:


The output for each test case should contain a line of the form “Case #X: “ where X is the number of the test case.


The next lines should contain the merged intervals in the ascending order.


C++ Code:




#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int A[100],B[100],t,a,b,n,d=1;
cin>>t;
while(t--)
{
int m=1;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>A[i];
cin>>B[i];
}
sort(A,A+n);
sort(B,B+n);
cin>>a>>b;
int j=0;
while(j<=n-1 && a>A[j] && a>B[j])
{
j++;
}
int k=n-1;
while(k>=0 && b<B[k] && b<A[k])
{
k--;
}
if(j<k)
{
if(a<A[j])
{
A[j]=a;
}
if(b>B[k])
{
B[j]=b;
}
else
{
B[j]=B[k];
}
}
else if(j>k)
{
m=0;
}
else
{
if(B[j]<b)
{
B[j]=b;
}
}
if(m==1)
{
cout<<"Case #"<<d<<": "<<endl;
for(int i=0;i<=j;i++)
{
cout<<A[i]<<" "<<B[i]<<endl;
}
for(int i=k+1;i<n;i++)
{
cout<<A[i]<<" "<<B[i]<<endl;
}
}
else
{
cout<<"Case #"<<d<<": "<<endl;
for(int i=0;i<j;i++)
{
cout<<A[i]<<" "<<B[i]<<endl;
}
cout<<a<<" "<<b<<endl;
for(int i=k+1;i<n;i++)
{
cout<<A[i]<<" "<<B[i]<<endl;
}
}
d++;
}
return 0;
}
Sample Input:
1
6
1 5
6 15
20 21
23 26
27 30
35 40
14 33
Sample Output:
Case #1:
1 5
6 33
35 40

Thursday, January 19, 2012

Find the next higher number with the same digits


//Code to find the next higher number with the same digits.
#include<iostream>
using namespace std;
int main()
{
long long int m,n,A[1000],i,j,k,temp,same;
cin>>n;
same=0;
i=0;
while(n>0)
{
A[i]=n%10;
n=n/10;
i++;
}
//Finding the first digit at which the next digit is less than this digit starting from the left.
for(j=0;j<i-1;j++)
{
if(A[j+1]<A[j])
{
//Finding the smallest digit greater than the pivotal digit.
for(k=0;k<=j;k++)
{
if(A[k]>A[j+1])
{
break;
}
}
//Swapping the two digits.
temp=A[k];
A[k]=A[j+1];
A[j+1]=temp;
same=1;
break;
}
}
//If the next higher number is possible with the same number of digits
if(same==1)
{
m=A[i-1];
for(k=i-2;k>j;k--)
{
m=m*10+A[k];
}
for(j=0;j<=k;j++)
{
m=m*10+A[j];
}
}
//If the next higher number needs to have greater number of digits.
else
{
m=A[0];
m=m*10+A[0];
for(j=1;j<i;j++)
{
m=m*10+A[j];
}
}
cout<<m<<endl;
fflush(stdin);
getchar();
return 0;
}
Input: 37723971
Output: 37727139

Tuesday, December 6, 2011

C++ Program to pick the scan codes from a keyboard




  1. /* This program shows how to pick up the scan codes from a keyboard */




  2. /* These define the scan codes(IBM) for the keys. All numbers are in decimal.*/


  3. #define PAGE_UP 73


  4. #define HOME 71


  5. #define END 79


  6. #define PAGE_DOWN 81


  7. #define UP_ARROW 72


  8. #define LEFT_ARROW 75


  9. #define DOWN_ARROW 80


  10. #define RIGHT_ARROW 77


  11. #define F1 59


  12. #define F2 60


  13. #define F3 61


  14. #define F4 62


  15. #define F5 63


  16. #define F6 64


  17. #define F7 65


  18. #define F8 66


  19. #define F9 67


  20. #define F10 68


  21. #include <iostream>


  22. #include <conio.h>




  23. using namespace std;




  24. int main()


  25. {


  26. char KeyStroke;




  27. cout << "Press Escape to quit." << endl;




  28. do


  29. {


  30. KeyStroke = getch();




  31. if (KeyStroke == 0)


  32. {


  33. KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke


  34. switch (KeyStroke)


  35. {


  36. case PAGE_UP:


  37. cout << "PAGE UP" << endl;


  38. break;


  39. case PAGE_DOWN:


  40. cout << "PAGE DOWN" << endl;


  41. break;


  42. case HOME:


  43. cout << "HOME" << endl;


  44. break;


  45. case END:


  46. cout << "END" << endl;


  47. break;


  48. case UP_ARROW:


  49. cout << "UP ARROW" << endl;


  50. break;


  51. case DOWN_ARROW:


  52. cout << "DOWN ARROW" << endl;


  53. break;


  54. case LEFT_ARROW:


  55. cout << "LEFT_ARROW" << endl;


  56. break;


  57. case RIGHT_ARROW:


  58. cout << "RIGHT_ARROW" << endl;


  59. break;


  60. case F1:


  61. cout << "F1" << endl;


  62. break;


  63. case F2:


  64. cout << "F2" << endl;


  65. break;


  66. case F3:


  67. cout << "F3" << endl;


  68. break;


  69. case F4:


  70. cout << "F4" << endl;


  71. break;


  72. case F5:


  73. cout << "F5" << endl;


  74. break;


  75. case F6:


  76. cout << "F6" << endl;


  77. break;


  78. case F7:


  79. cout << "F7" << endl;


  80. break;


  81. case F8:


  82. cout << "F8" << endl;


  83. break;


  84. case F9:


  85. cout << "F9" << endl;


  86. break;


  87. case F10:


  88. cout << "F10" << endl;


  89. break;


  90. default:


  91. cout << "Some other key." << endl;


  92. }


  93. }


  94. else


  95. cout << KeyStroke << endl;


  96. }


  97. while (KeyStroke != 27); // 27 = Escape key


  98. }


Wednesday, March 30, 2011

The Twin Towers(Longest Common Subsequence)

#include<iostream>
using namespace std;
int main()
{
int A[102],B[102],C[102][102],m,n,count=1,i,j;
void fun(int[],int[],int[][102],int,int);
for(i=0;i<102;i++)
{
C[0][i]=0;
C[i][0]=0;
}
while(cin>>m)
{
if(m==0)
{
cin>>n;
if(n==0)
{
break;
}
}
else
{
cin>>n;
for(i=1;i<=m;i++)
{
cin>>A[i];
}
for(i=1;i<=n;i++)
{
cin>>B[i];
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(A[i]==B[j])
{
C[i][j]=C[i-1][j-1]+1;
}
else
{
if(C[i][j-1]>C[i-1][j])
{
C[i][j]=C[i][j-1];
}
else
{
C[i][j]=C[i-1][j];
}
}
}
}
cout<<"Twin Towers #"<<count<<endl;
cout<<"Number of Tiles : "<<C[m][n]<<endl;
count++;
fun(A,B,C,m,n);
cout<<endl;
}
}
return 0;
}
void fun(int A[],int B[],int C[][102],int i,int j)
{
if(i==0 || j==0)
{
return;
}
else
{
if(C[i-1][j-1]==C[i][j]-1)
{
fun(A,B,C,i-1,j-1);
cout<<A[i]<<" ";
}
else if(C[i-1][j]==C[i][j])
{
fun(A,B,C,i-1,j);
}
else
{
fun(A,B,C,i,j-1);
}
}
}

Sunday, January 9, 2011

Point in Triangle

The Problem

You are given the coordinates of the vertices of a triangle and also the coordinates of a point. You have to determine whether the point lies in the triangle or the it lies outside the triangle.

Logic Used

Distance of the point from a vertex is compared with the distances of the vertex from the other two vertices. If the distance of the point is found to be greater than any of the distances from other vertices, then the point lies outside the triangle. This is repeated for all the three vertices of the triangle.


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float dist(float,float,float,float);//Function to find the distance between two points.
float x1,x2,x3,y1,y2,y3,px,py,distance,length1,length2;
int out=0;
while(cin>>x1)
{
cin>>y1;
cin>>x2>>y2;
cin>>x3>>y3;
cin>>px>>py;
out=0;
length1=dist(x1,y1,x2,y2);
length2=dist(x1,y1,x3,y3);
distance=dist(x1,y1,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
length1=dist(x2,y2,x1,y1);
length2=dist(x2,y2,x3,y3);
distance=dist(x2,y2,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
length1=dist(x3,y3,x1,y1);
length2=dist(x3,y3,x2,y2);
distance=dist(x3,y3,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
if(out==0)
{cout<<"Inside"<<endl;}
else
{cout<<"Outside"<<endl;}
}
return 0;
}
float dist(float x1,float y1,float x2,float y2)
{
return sqrt(pow((x2-x1),2)+pow((y2-y1),2));
}

#include<iostream>


#include<cmath>


using namespace std;


int main()


{


float dist(float,float,float,float);//Function to find the distance between two points.


float x1,x2,x3,y1,y2,y3,px,py,distance,length1,length2;


int out=0;


while(cin>>x1)


{


cin>>y1;


cin>>x2>>y2;


cin>>x3>>y3;


cin>>px>>py;


out=0;


length1=dist(x1,y1,x2,y2);


length2=dist(x1,y1,x3,y3);


distance=dist(x1,y1,px,py);


if(distance>length1 || distance>length2)


{


out=1;


}


length1=dist(x2,y2,x1,y1);


length2=dist(x2,y2,x3,y3);


distance=dist(x2,y2,px,py);


if(distance>length1 || distance>length2)


{


out=1;


}


length1=dist(x3,y3,x1,y1);


length2=dist(x3,y3,x2,y2);


distance=dist(x3,y3,px,py);


if(distance>length1 || distance>length2)


{


out=1;


}


if(out==0)


{cout<<"Inside"<<endl;}


else


{cout<<"Outside"<<endl;}


}


return 0;


}


float dist(float x1,float y1,float x2,float y2)


{


return sqrt(pow((x2-x1),2)+pow((y2-y1),2));


}