Posts

Showing posts with the label Recursion

Printing all the permutation of the string using recursion

           dog    dog ,  odg ,  god    (in first call 1st character is swapped with all postions (1st to last)) dgo     ogd       gdo   (in 2nd call ,2nd character is swapped with all postions(2nd to last) ) #include<bits/stdc++.h> using namespace std; void permu(string k,int fst,int lst) {     if(fst==lst)         cout<<k<<endl;     for(int i=fst;i<=lst;i++)     {         swap(k[fst],k[i]);         permu(k,fst+1,lst);         swap(k[fst],k[i]);     } } int main() {     string s;     cin>>s;     int lst=s.size()-1;     permu(s,0,lst);     return 0; }

Reverse String using Recursion C++

Here the Logic is simple to print the last character of the string for each function call and again call the reverse function again ,this time the string doesn't contain last character. #include<bits/stdc++.h> using namespace std; void reve(string k) {   int sz=k.size();   if(sz==1)     cout<<k[sz-1];   else   {      cout<<k[sz-1];      string p=k.substr(0,sz-1);      reve(p);   } } int main() {     string s;     cin>>s;     reve(s);     return 0; }

No. of subsets whose gcd is 1

                                                                     Subtraction Game 2                                       https://www.codechef.com/problems/AMSGAME2                        It is only a branch and bound type of recursion. #include<bits/stdc++.h> using namespace std; long long int n,i; long long int a [ 1000 ] ; long long int dp [ 70 ] [ 50010 ] ; long long int go ( long long int cur_indx, long long int cur_gcd ) { if ( cur_indx>=n && cur_gcd== 1 ) return 1 ; else if ( cur_indx>=n && cur_gcd!= 1 ) return 0 ; if ( dp [ cur_ind...

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.

http://codeforces.com/contest/476/problem/A #include < bits / stdc ++. h > using namespace std ; int dp [ 10001 ][ 5055 ]; int fun ( int cu , int n , int m , int st ) { if ( st % m == 0 && cu == n ) { // cout<<st<<endl; return st ; } if ( cu > n || st > n || st > 5050 ) return 999999 ; int & ret = dp [ cu ][ st ]; if ( ret ) return ret ; int k1 = fun ( cu + 1 , n , m , st + 1 ); int k2 = fun ( cu + 2 , n , m , st + 1 ); return ret = min ( k1 , k2 ); } int main () { int n , m , i , j , k , l ; cin >> n >> m ; //if(n>m*2) // cout<<-1<<endl; k = fun ( 0 , n , m , 0 ); if ( k == 999999 ) cout <<- 1 << endl ; else cout << k << endl ; return 0 ; }

B. Dreamoon and WiFi :calculate no. of ways : recursive solution (branch and bound )

http://codeforces.com/contest/476/problem/B #include < bits / stdc ++. h > using namespace std ; string s1 , s2 ; int val = 0 , n ; int dp [ 15 ][ 15 ]; int solve ( int s , int l , int f ) { int & ret = dp [ s ][ l ]; if ( s == l ) { if ( f == val ) return 1 ; else return 0 ; } int ct = 0 ; ///number of ways in starting is 0 if ( s2 [ s ]== '+' ) ct += solve ( s + 1 , l , f + 1 ); if ( s2 [ s ]== '-' ) ct += solve ( s + 1 , l , f - 1 ); if ( s2 [ s ]== '?' ) ct += solve ( s + 1 , l , f + 1 )+ solve ( s + 1 , l , f - 1 ); return ret = ct ; /// In the last return this value } int main () { int i , j , k , l ; double ans ; cin >> s1 >> s2 ; n = s1 . size (); for ( i = 0 ; i < n ; i ++) { if ...

(Repetitive numbers allowed "for loop recursion " )Print all possible strings of length k that can be formed from a set of n characters

Input: set[] = {'a', 'b'}, k = 3 Output: aaa aab aba abb baa bab bba bbb Input: set[] = {'a', 'b', 'c', 'd'}, k = 1 Output: a b c d This is a "branch and bound" type of Recursion in which we have to choose whether to "take a no." or  "not to take" that number . Here Repetition is allowed ,therefore a "for loop" is needed. #include<bits/stdc++.h> using namespace std; set<vector<char> > s; void gen(char a[],int n,int k,int cnt,vector<char> v,int j) {     if(cnt==k)     {      s.insert(v);     }     if(j>=n)         return;     for(int i=0;i<n;i++)     {         gen(a,n,k,cnt,v,j+1);         v.push_back(a[i]);         gen(a,n,k,cnt+1,v,j+1);         v.pop_back();     } } int main() {  char a[]={'a','b','c','d'};  int n,k; ...

NON Repeatitive ::: -- Print all increasing sequences of length k from first n natural numbers (Recursive)

http://www.geeksforgeeks.org/print-increasing-sequences-length-k-first-n-natural-numbers/ Geeksforgeeks. Input: k = 2, n = 3 Output: 1 2 1 3 2 3 Input: k = 5, n = 5 Output: 1 2 3 4 5 Input: k = 3, n = 5 Output: 1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5 This is a Branch and Bound type of Problem. In which you have to decide whether to "take a number" or "not to take" a number. After that make a recursive call again. #include<bits/stdc++.h> using namespace std; int fi(int dest,int steps,int cu) { if(cu==dest) return steps; if(abs(cu)>dest) return INT_MAX; return min(fi(dest,steps+1,steps+cu+1),fi(dest,steps+1,cu-steps-1)); } int main() { int dest,i,j; cin>>dest; cout<<fi(dest,0,0)<<endl; return 0; }

Minimum Steps to reach a destination (Recursive method )

http://www.geeksforgeeks.org/minimum-steps-to-reach-a-destination/ GeeksforGeeks. It is a ""branch and bound"" type of recursive problem, in which we have two options of going either left or Right in a number line.  And the target is to reach a particular point. Starting Point is Zero. #include<bits/stdc++.h> using namespace std; int fi(int dest,int steps,int cu) {  if(cu==dest)     return steps;  if(abs(cu)>dest)     return INT_MAX;   return min(fi(dest,steps+1,steps+cu+1),fi(dest,steps+1,cu-steps-1)); } int main() { int dest,i,j; cin>>dest; cout<<fi(dest,0,0)<<endl; return 0; }

Spoj Problem--> Coins Recursive method.

                                            http://www.spoj.com/problems/COINS/ COINS - Bytelandian gold coins A Coin at a Moment can be changed to three coins (n/2) ,(n/3) , (n/4). Here Map is used to save the  result in a current state ( dp ) , The saved result can be used next time it is used, #include<bits/stdc++.h> using namespace std; map<long long int,long long int > dp; long long int brk(long long int n) {     if(dp[n])         return dp[n];     if(n<12)         return n;    long long int ct=0;   if(n>=12)     {       ct+=brk(n/2);       ct+=brk(n/3);       ct+=brk(n/4);     }   return dp[n]=ct; } int main() {  long long int ...

Program to find total number of Paths between two points in a graph.( Recursive method )

If the "source" Pointer will become "destination" (after the cursion calls ) the path array will get printed. #include<bits/stdc++.h> using namespace std; int fin(vector<int> a[],int src,int dest,vector<int> path,int visited[]) {     path.push_back(src);     if(src==dest)     {       for(int i=0;i<path.size();i++)              cout<<path[i]<<" ";                cout<<endl;                return 1;     }     visited[src]=1;     int ct=0;     for(int i=0;i<a[src].size();i++)         if(visited[a[src][i]]==0)             ct+=fin(a,a[src][i],dest,path,visited);      visited[src]=0;      return ct; } int main() {  vector<int> v[...
Recusive Dp (Branch & Bound ) Strategy for the World Cup (codechef ) In this question we have to complete a given amount of runs using only 4's and 6's  we have to say the no. of ways by which we can do this.  in this question there are four cases  case 1) run=6    then--->> ball= ball-1 , run=run-6, wicket=wicket  case 2) run=4     then--->> ball= ball-1 , run=run-4, wicket=wicket  case 3) run=0     then--->> ball= ball-1 , run=run, wicket=wicket  case 4) run=out  then--->> ball= ball-1 , run=run, wicket=wicket-1 base case if (run==0 && bal==0 && w>=0 ) this will return 1; #include<bits/stdc++.h> using namespace std; long long  dp[305][1805][15]; long long  solve(long long  b,long long  r,long long  w) {   long long  a=0;   if(r==0 && b==0 && w>=0)   return 1; ...
Recursion  Questions  (Branch & Bound) The Game Of Weights (codechef ) In this question , there are two cases   case1) when barrier[i] < total barrier  ( here we have two options either we can take this or leave this) case2 ) when barrier[i] > total barrier (here we must have to take this) Base condition of this recursion , if we reached at last position ,                                                               and if barrier[i] < total barrier then                                                                               return total cost.          ...