Thursday, January 17, 2013

C++ program for jumps problem

Given an array of positive numbers, find the number of ways to reach the end of the array in minimum number of steps where the value at each index represents the maximum number of steps one can move.
Sample Input:
The first line contains a number n, the number of elements in the array. The second line contains n space separated numbers.
11
1 3 5 8 9 2 6 7 6 8 9
Sample Output
Output should contain n space separated numbers, each represents the number of ways at that index.
2 2 4 1 1 2 1 1 1 1 0
#include<iostream>
using namespace std;
int main()
{
int n,A[100];
cin>>n;
int max=-9999999;
for(int i=0;i<n;i++)
{
cin>>A[i];
if(A[i]>max)
{
max=A[i];
}
}
//cout<<max<<endl;
cout<<"The given array is:\n";
for(int i=0;i<n;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
int B[100]={0};
for(int i=0;i<n-1;i++)
{
B[i]=A[i];
}
for(int i=n-2;i>=0;i--)
{
if(i+A[i] >= n-1)
{
B[i]=1;
}
else
{
int min=max;
bool inside = false;
for(int j=i+A[i];j>i;j--)
{
if(B[j]!=0 && B[j]<=min)
{
min=B[j];
inside = true;
}
}
if(inside)
{
B[i]=min+1;
}
}
}
cout<<"The distance array is:\n";
for(int i=0;i<n;i++)
{
cout<<B[i]<<" ";
}
cout<<endl;
int num[100]={0};
for(int i=n-2;i>=0;i--)
{
if(B[i]==1)
{
num[i]=B[i];
}
else
{
for(int j=1;i+j<n && j<=A[i];j++)
{
if(B[i+j]==B[i]-1)
{
//cout<<i<<" "<<B[i]<<" "<<j<<endl;
num[i]+=num[i+j];
}
}
}
}
cout<<"Number of ways:\n";
for(int i=0;i<n;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
}
11
1 3 5 8 9 2 6 7 6 8 9
The given array is:
1 3 5 8 9 2 6 7 6 8 9
The distance array is:
3 2 2 1 1 2 1 1 1 1 0
Number of ways:
2 2 4 1 1 2 1 1 1 1 0

Saturday, January 12, 2013

C++ program to find all the possible interleavings of two input strings


#include<iostream>
#include<string.h>
#include<map>
using namespace std;
map<string,bool> myMap;
void generateInterleaved(char A[100],char B[100],char out[200],int start1,int start2)
{
if(strlen(out)==strlen(A)+strlen(B))
{
if(myMap.find(out) == myMap.end())
{
myMap[out]=true;
}
return;
}
else
{
if(start1<strlen(A))
{
out[strlen(out)]=A[start1];
generateInterleaved(A,B,out,start1+1,start2);
out[strlen(out)-1]=0;
}
if(start2<strlen(B))
{
out[strlen(out)]=B[start2];
generateInterleaved(A,B,out,start1,start2+1);
out[strlen(out)-1]=0;
}
}
}
int main()
{
char A[100],B[100],C[100];
bool isInterleaved(char[],char[],char[]);
cin>>A>>B>>C;
bool result=isInterleaved(A,B,C);
if(result==true)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
char out[200]={0};
cout<<"The possible interleavings are:\n";
generateInterleaved(A,B,out,0,0);
for(map<string,bool>::iterator it = myMap.begin();it != myMap.end(); ++it)
{
cout<<it->first<<endl;
}
}
bool isInterleaved(char A[100],char B[100],char C[100])
{
int j=0,k=0;
for(int i=0;i<strlen(C);i++)
{
if(j<strlen(A) && C[i]==A[j])
{
j++;
}
else if(k<strlen(B) && C[i]==B[k])
{
k++;
}
}
if(j==strlen(A) && k==strlen(B))
{
return true;
}
else
{
return false;
}
}
Input:
ab
cd
Output:
ab
cd
abcd
YES
The possible interleavings are:
abcd
acbd
acdb
cabd
cadb
cdab