Question
Can you solve this useing bottom-top approach (Dynamic Programin) if possible
6.6. Let us define a multiplication operation on three symbols a, b, c according to the following table; thus ab = b, ba = c,
0 0
Add a comment Improve this question Transcribed image text
Answer #1

bottom-top approaches are more difficult to understand because you have to analyse that well , so due to limited time i'm going to use top- down approach becuase that is more intitutive.

so we will create a recursive function for this which will take two arguments one is string and another required output character.

now main part is that 'a', 'b', 'c' can occur with only 3 combination each so we can check combination of different substring and find wheter particular character can occur.

I've implemented a recursive definition for this have a look:

 #include<iostream> using namespace std; char matrix[3][3]={{'b','b','a'},{'c','b','a'},{'a','c','c'}}; bool charmult(string s,char a) {   int len=s.length();     if(len==0)              return 0;       if(len==1) //base case  {               return s[0]==a; //check if s[0] is character a  }       bool check=0;   for(int i=1;i<len;i++) //iterate the substring from 1 to last        {               string first=s.substr(0,i); //first string from 0 to i          string second=s.substr(i,len-i); //second string from i to last                 switch(a)               {                       case 'a':{ //when we want the combination to be a                               check=check || (charmult(first,'a')&&charmult(second,'c'));                             check=check || (charmult(first,'b')&&charmult(second,'c'));                             check=check || (charmult(first,'c')&&charmult(second,'a'));                             break;                  }                       case 'b':{ //when we want the combination to be b                               check=check || (charmult(first,'a')&&charmult(second,'a'));                             check=check || (charmult(first,'a')&&charmult(second,'b'));                             check=check || (charmult(first,'b')&&charmult(second,'b'));                             break;                  }                       case 'c':{ ////when we want the combination to be c                             check=check || (charmult(first,'b')&&charmult(second,'a'));                             check=check || (charmult(first,'c')&&charmult(second,'b'));                             check=check || (charmult(first,'c')&&charmult(second,'c'));                             break;                  }               }               if(check)                       break;  }       return check; } int main() {    string s="bbbbbbbb";    bool c=charmult(s,'a');         if(c) cout<<"yes"; else cout<<"no"; }

it's very easy to understand we are dynamically checking combinations and this algorithm runs in O( n2 )

I hope this will help you so please give positive ratings:)))

Add a comment
Know the answer?
Add Answer to:
Can you solve this useing bottom-top approach (Dynamic Programin) if possible 6.6. Let us define a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT