Tuesday, November 13, 2012

Inorder, Preorder and Postorder Successor and Predecessor Functions inC++


#include<iostream>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
struct node* next;
};
struct node* createnode(int x)
{
struct node* new_node=new struct node;
new_node->data=x;
new_node->left=NULL;
new_node->right=NULL;
new_node->next=NULL;
return new_node;
}
void inord_succ(struct node* root,struct node** prev)
{
if(root)
{
inord_succ(root->left,prev);
if(*prev!=NULL)
{
(*prev)->next = root;
*prev = root;
}
else
{
*prev = root;
}
inord_succ(root->right,prev);
}
}
void inord_pred(struct node* root,struct node** prev)
{
if(root)
{
inord_pred(root->left,prev);
root->next=*prev;
*prev=root;
inord_pred(root->right,prev);
}
}
void preord_succ(struct node* root,struct node** prev)
{
if(root)
{
if(*prev!=NULL)
{
(*prev)->next=root;
*prev=root;
}
else
{
*prev=root;
}
preord_succ(root->left,prev);
preord_succ(root->right,prev);
}
}
void preord_pred(struct node* root,struct node** prev)
{
if(root)
{
root->next=*prev;
*prev=root;
preord_pred(root->left,prev);
preord_pred(root->right,prev);
}
}
void postord_succ(struct node* root,struct node** prev)
{
if(root)
{
postord_succ(root->left,prev);
postord_succ(root->right,prev);
if(*prev!=NULL)
{
(*prev)->next=root;
*prev=root;
}
else
{
*prev=root;
}
}
}
void postord_pred(struct node* root,struct node** prev)
{
if(root)
{
postord_pred(root->left,prev);
postord_pred(root->right,prev);
root->next=*prev;
*prev=root;
}
}
void inorder(struct node* root)
{
if(root)
{
inorder(root->left);
cout<<root->data<<" ";
if(root->next)
{
cout<<root->next->data<<endl;
}
else
{
cout<<endl;
}
inorder(root->right);
}
}
void preorder(struct node* root)
{
if(root)
{
cout<<root->data<<" ";
if(root->next)
{
cout<<root->next->data<<endl;
}
else
{
cout<<endl;
}
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node* root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
if(root->next)
{
cout<<root->next->data<<endl;
}
else
{
cout<<endl;
}
}
}
int main()
{
struct node* root=createnode(1);
root->left=createnode(2);
root->right=createnode(3);
root->left->right=createnode(4);
root->left->left=createnode(5);
root->right->right=createnode(6);
root->right->left=createnode(7);
root->right->right->left=createnode(8);
//root->right->right->left->left=createnode(10);
root->right->right->right=createnode(10);
struct node* ptr = NULL;
//inord_succ(root,&ptr);
//inord_pred(root,&ptr);
//inorder(root);
//preord_succ(root,&ptr);
//preord_pred(root,&ptr);
//preorder(root);
//postord_succ(root,&ptr);
postord_pred(root,&ptr);
postorder(root);
cout<<endl;
}

Tuesday, November 6, 2012

Number of closed rectangles in a matrix

Given a matrix containing only 1 and 0, find the number of closed rectangles. A closed rectangle is represented by at least one 0 surrounded by eight 1's. There is no overlap of closed rectangles.

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int rectangle(int A[][10],int i,int j,int d1,int d2,int cnt);
int A[][10]={
{0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,0,0,0},
{0,0,1,0,0,0,1,0,0,0},
{0,0,1,0,0,0,1,0,0,0},
{0,0,1,1,1,1,1,0,0,0},
{1,1,1,0,0,0,1,1,1,1},
{0,0,1,0,0,0,1,0,1,1},
{1,1,1,0,0,0,1,1,1,1}
};
int cnt=0;
for(int i=0;i<8;i++)
{
for(int j=0;j<10;j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
for(int i=0;i<8;i++)
{
for(int j=0;j<10;j++)
{
if(A[i][j]>0)
{
cnt=rectangle(A,i,j,8,10,cnt);
}
}
}
cout<<"The number of closed rectangles is: "<<cnt<<endl;
}

int rectangle(int A[][10],int i,int j,int d1,int d2,int cnt)
{
cnt++;
int rnd=1;
if(i<d1-1 && j<d2-1 && A[i+1][j+1]==0)
{
j++;
while(j<d2 && i<d1-1 && A[i][j]==1 && A[i+1][j]==0)
{
A[i][j]=-1*cnt;
j++;
}
A[i][j]=-1*cnt;
rnd++;
}
if(i<d1-1 && j>0 && A[i+1][j-1]==0 && A[i][j]==-1*cnt)
{
i++;
while(i<d1 && j>0 && A[i][j]==1 && A[i][j-1]==0)
{
A[i][j]=-1*cnt;
i++;
}
A[i][j]=-1*cnt;
rnd++;
}
if(i>0 && j>0 && A[i-1][j-1]==0 && A[i][j]==-1*cnt)
{
j--;
while(j>=0 && i>0 && A[i-1][j]==0 && A[i][j]==1)
{
A[i][j]=-1*cnt;
j--;
}
A[i][j]=-1*cnt;
rnd++;
}
if(i>0 && j<d2-1 && A[i-1][j+1]==0 && A[i][j]==-1*cnt)
{
i--;
while(i>=0 && j<d2-1 && A[i][j+1]==0 && A[i][j]==1)
{
A[i][j]=-1*cnt;
i--;
}
A[i][j]=-1*cnt;
rnd++;
}
if(rnd!=5 || (A[i][j]!=0 && A[i][j]!=A[i][j+1]))
{
cnt--;
}
return cnt;
}
Output:
The number of closed rectangles is: 2

Friday, October 19, 2012

Android Translation Apps

Useful links are as follow:
1. Using Bing translation API:
http://www.microsoft.com/web/post/using-the-free-bing-translation-apis
2. Using Google Translation API:
http://rupeshpatel.wordpress.com/2012/06/23/usage-of-google-translator-api-for-free/
3. Link for Google Translator App:
https://play.google.com/store/apps/details?id=com.google.android.apps.translate&hl=en
4. Link for iTranslate App:
https://play.google.com/store/apps/details?id=at.nk.tools.iTranslate&hl=en
5. A nice Translator App with video demo:
http://www.cellictica.com/products.html#
6. List of Translator Apps:
http://android.appstorm.net/roundups/utilities-roundups/translation-apps-roundup/

Using Text to Speech API in Android

Useful links are as follow:
1. App developed using the tts engine:
http://mobile.tutsplus.com/tutorials/android/android-sdk-using-the-text-to-speech-engine/
2. Android link to use the tts API:
http://developer.android.com/reference/android/speech/tts/TextToSpeech.html
3. Android Sample Text-to-Speech API usage:
http://android-developers.blogspot.in/2009/09/introduction-to-text-to-speech-in.html
4. Voice Recorder App in Android:
https://play.google.com/store/apps/details?id=com.tokasiki.android.voicerecorder&hl=en

Using Speech to Text API in Android

Useful links are as follow:
1. Good google search string: speech to text api android
2. Android emulator is not capable of audio record:
http://www.deveature.com/2011/11/28/speech-recognizer-does-not-work-on-android-emulator/
3. Audio-capture in android:
http://developer.android.com/guide/topics/media/audio-capture.html
4. Stack overflow link for using MediaRecorder in Android:
http://stackoverflow.com/questions/5254994/can-the-android-emulator-record-and-play-back-audio-using-pc-hardware
5. App developed using Recognizer Intent (speech-to-text API):
http://viralpatel.net/blogs/android-speech-to-text-api/
6. Speech Input API for Android:
http://android-developers.blogspot.in/2010/03/speech-input-api-for-android.html
7. Link for Android Sample Projects:
http://developer.android.com/tools/samples/index.html
8. Recognizer Intent in Android:
http://developer.android.com/reference/android/speech/RecognizerIntent.html
9. SDK Version Concept in Android:
http://developer.android.com/guide/topics/manifest/uses-sdk-element.html
10. Voice Search app in Android:
https://play.google.com/store/apps/details?id=com.google.android.voicesearch&feature=search_result

Saturday, September 29, 2012

Two Ends

In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest — we’ll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.)

3 2 10 4

You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.

Input


There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.

Output


For each test case you should print one line of output of the form:

In game m, the greedy strategy might lose by as many as p points.

where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player’s score and second player’s score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.

Example


Input:
4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0

Output:
In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.

#include<iostream>
using namespace std;
int max=0;
int main()
{
int A[100],n,game=1;
int fun(int A[],int low,int high,int s1,int s2,int turn);
while(cin>>n)
{
if(n==0)
{
break;
}
else
{
for(int i=0;i<n;i++)
{
cin>>A[i];
}
int turn=1,low=0,high=n-1,s1=0,s2=0;
::max=0;
::max=fun(A,low,high,s1,s2,turn);
}
cout<<"In game "<<game<<", the greedy strategy might lose by as many as "<<::max<<" points."<<endl;
game++;
}
return 0;
}
int fun(int A[],int low,int high,int s1,int s2,int turn)
{
//static int max=0;
if(low>high)
{
if(::max<s1-s2)
{
::max=s1-s2;
}
return ::max;
}
else if(turn==1)
{
s1+=A[low];
turn=2;
fun(A,low+1,high,s1,s2,turn);
s1-=A[low];
s1+=A[high];
fun(A,low,high-1,s1,s2,turn);
}
else if(turn==2)
{
turn=1;
if(A[low]>=A[high])
{
s2+=A[low];
fun(A,low+1,high,s1,s2,turn);
}
else
{
s2+=A[high];
fun(A,low,high-1,s1,s2,turn);
}
}
}

Counting discrete clusters of 1 in a binary 2D matrix


#include<iostream>
using namespace std;
int main()
{
int A[100][100];
int m,n;
void fun(int A[][100],int i,int j,int c,int m,int n);
cin>>m>>n;
int c=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>A[i][j];
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(A[i][j]>0)
{
c++;
fun(A,i,j,c,m,n);
}
}
}
cout<<c<<endl;
return 0;
}
void fun(int A[][100],int i,int j,int c,int m,int n)
{
A[i][j]=-1*c;
if(i>0 && j>0 && A[i-1][j-1]>0)
{
fun(A,i-1,j-1,c,m,n);
}
if(i>0 && j>=0 && j<n && A[i-1][j]>0)
{
fun(A,i-1,j,c,m,n);
}
if(i>0 && j<n-1 && A[i-1][j+1]>0)
{
fun(A,i-1,j+1,c,m,n);
}
if(i>=0 && i<n && j<n-1 && A[i][j+1]>0)
{
fun(A,i,j+1,c,m,n);
}
if(i<n-1 && j<n-1 && A[i+1][j+1]>0)
{
fun(A,i+1,j+1,c,m,n);
}
if(i<n-1 && j<n && j>=0 && A[i+1][j]>0)
{
fun(A,i+1,j,c,m,n);
}
if(i<n-1 && j>0 && A[i+1][j-1]>0)
{
fun(A,i+1,j-1,c,m,n);
}
if(i<n && i>=0 && j>0 && A[i][j-1]>0)
{
fun(A,i,j-1,c,m,n);
}
}
Sample Input:
3 5
1 1 1 0 0
0 0 1 0 1
0 1 0 1 1

Output:
1