Sunday, July 22, 2012

C Program to write the permutations of the characters of a string using Recursion


#include<stdio.h>
#include<string.h>
int main()
{
void permute(char[],char[],int[],int,int);
char A[100];
scanf("%s",A);
char B[100]="";
int length=strlen(A);
int level=0;
int used[100]={0};
permute(A,B,used,length,level);
return 0;
}
void permute(char* A,char* B,int* used,int length,int level)
{
if(level==length)
{
B[level]='';
printf("%s\n",B);
return;
}
else
{
for(int i=0;i<length;i++)
{
if(used[i]==1)
{
continue;
}
used[i]=1;
B[level]=A[i];
permute(A,B,used,length,level+1);
used[i]=0;
B[level]='';
}
return;
}
}

Saturday, July 21, 2012

Alternating Letters

An alternation in a string is defined to be two distinct characters (letters, digits, or punctuation) that alternate at least three times as the word is read from left to right. The word “boo” has no alternations, but “booboo” has “bobo” as an alternation of length 4. Upper and lower case characters are considered to be different, and so, “aragorn” contains the 4-element alternation “arar”, but “Aragorn” only has the 3-element alternation “rar”. Digits and punctuation may be used, so “a2b-c2d-” has the alternation “2-2-”. The objective of this problem is to write a program to find the length of the longest alternation in a word.
Examples:












































InputOutputExplanation
exasperatedLength = 5eaeae
counterintuitiveLength = 6tititi
insignificantLength = 6ininin
AragornLength = 3rar
a2b-c2d-Length = 42-2-
catLength = 0 No subsequence having length > 3
caLength = 0 No subsequence having length > 3


#include<stdio.h>
#include<string.h>
int main()
{
int A[256]={0},max=0,B[256]={0};
char s[1000];
scanf("%s",s);
for(int i=0;i<strlen(s);i++)
{
A[s[i]]++;
if(s[i]>max)
{
max=s[i];
}
}
int k=0;
for(int i=0;i<=max;i++)
{
if(A[i]!=0)
{
B[k]=i;
k++;
}
}
int maxcount=0;
for(int i=0;i<k;i++)
{
for(int j=i+1;j<k;j++)
{
int count=0;
int order=0;
char curr='';
for(int c=0;c<strlen(s);c++)
{
if(s[c]==B[i])
{
if(order==0)
{
count++;
order=1;
curr=B[i];
}
else if(curr==B[j])
{
count++;
curr=B[i];
}
}
else if(s[c]==B[j])
{
if(order==0)
{
count++;
order=1;
curr=B[j];
}
else if(curr==B[i])
{
count++;
curr=B[j];
}
}
}
if(maxcount<count)
{
maxcount=count;
}
}
}
if(maxcount>=3)
{
printf("%d\n",maxcount);
}
else
{
printf("0\n");
}
return 0;
}

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. }