Question

In mythology, the Hydra was a monster with many heads. Every time the hero chopped off a head, two smaller heads would grow i

c++ code
In mythology, the Hydra was a monster with many heads. Every time the hero chopped off a head, two smaller heads would grow i


In mythology, the Hydra was a monster with many heads. Every time the hero chopped off a head, two smaller heads would grow i
In mythology, the Hydra was a monster with many heads. Every time the hero chopped off a head, two smaller heads would grow in its place. Fortunately for the hero, If the head was small enough, he could chop it off without two more growing in its place. To kill the Hydra, all the hero needed to do was to chop off all the heads. Write a program that simulates the Hydra. Instead of heads, we will use strings. A container of strings, then, represents the Hydra. You can use any ADT we've learned to represent the container (Bog, List etc.). The book recommends Bag but that's not the only option. Irecommend using the Linked implementation of whatever ADT you choose though since you don't know the length you will need for the container. Every time you remove a string from the list, delete the first letter of the string and put two copies of the remaining string back into the list. For example, if you remove HYDRA, you add two copies of YDRA to the list. If you remove a one- letter word, you add nothing to the list. Exactly where in the list you add or remove strings is up to you. For example, you might always add to the end of the list, or you might add at random positions. To begin, read a word from the keyboard and place it into an empty list. The rule is only one string can be removed at a time. You can let the user choose which string to remove or you can have the program choose. Remember once a string is removed two strings must be added to your container that are a copies of the string removed with the first letter removed. The Hydra dies when the list has no more strings for you to remove Using Big O notation, predict the time requirement for this algorithm in terms of the number n of characters in the initial string. State what computation unt you used for your Big O analysis Try some benchmark analysis on various strings of size n to see if your Big O is correct. Look at my programs in Chapter 11 where I counted comparisons or Compare your results when you use the following strategies for removing a string from the list 1. Always remove the shortest string. 2. Always remove the longest string.
In mythology, the Hydra was a monster with many heads. Every time the hero chopped off a head, two smaller heads would grow in its place. Fortunately for the hero, If the head was small enough, he could chop it off without two more growing in its place. To kill the Hydra, all the hero needed to do was to chop off all the heads. Write a program that simulates the Hydra. Instead of heads, we will use strings. A container of strings, then, represents the Hydra. You can use any ADT we've learned to represent the container (Bog, List etc.). The book recommends Bag but that's not the only option. Irecommend using the Linked implementation of whatever ADT you choose though since you don't know the length you will need for the container. Every time you remove a string from the list, delete the first letter of the string and put two copies of the remaining string back into the list. For example, if you remove HYDRA, you add two copies of YDRA to the list. If you remove a one- letter word, you add nothing to the list. Exactly where in the list you add or remove strings is up to you. For example, you might always add to the end of the list, or you might add at random positions. To begin, read a word from the keyboard and place it into an empty list. The rule is only one string can be removed at a time. You can let the user choose which string to remove or you can have the program choose. Remember once a string is removed two strings must be added to your container that are a copies of the string removed with the first letter removed. The Hydra dies when the list has no more strings for you to remove Using Big O notation, predict the time requirement for this algorithm in terms of the number n of characters in the initial string. State what computation unt you used for your Big O analysis Try some benchmark analysis on various strings of size n to see if your Big O is correct. Look at my programs in Chapter 11 where I counted comparisons or Compare your results when you use the following strategies for removing a string from the list 1. Always remove the shortest string. 2. Always remove the longest string.
In mythology, the Hydra was a monster with many heads. Every time the hero chopped off a head, two smaller heads would grow in its place. Fortunately for the hero, if the head was small enough, he could chop it off without two more growing in its place. To kill the Hydra, the hero needed to do was to chop off all the heads. Write a program that simulates the Hydra. Instead of heads, we will use strings. A container of strings, then, represents the Hydra. You can use any ADT we've learned to represent the container (Bag. List etc.). The book recommends Bag but that's not the only option. I recommend using the Linked implementation of whatever ADT you choose though since you don't know the length you will need for the container Every time you remove a string from the list, delete the first letter of the string and put two copies of the remaining string back into the list. For example, if you remove HYDRA, you add two copies of YDRA to the list. If you remove a one- letter word, you add nothing to the list. Exactly where in the list you add or remove strings is up to you. For example, you might always add to the end of the list, or you might add at random positions. To begin, read a word from the keyboard and place it into an empty list. The rule is only one string can be removed at a time. You can let the user choose which string to remove or you can have the choose. Remember once a string is removed two strings must be added to your container that are a copies of the string removed with the first letter removed. The Hydra dies when the list has no more strings for you to remove. program Using Big O notation, predict the time requirement for this algorithm in terms of the number n of characters in the initial string. State what computation unit you used for your Big O analysis. Try some benchmark analysis on various strings of size n to see if your Big O is correct. Look at my programs in Chapter 11 where I counted comparisons or assignments. Compare your results when you use the following strategies for removing a string from the list: 1. Always remove the shortest string. 2. Always remove the longest string.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note that I will be using vector as a container which is a dynamic array in C++ that is it's size is increased automatically as required I will be removing 1st string at every step

Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

#include<bits/stdc++.h>
using namespace std;
void print(vector <string>v)
{
    for(int i=0;i<v.size();i++)
        cout<<v[i]<<" ";
    cout<<endl;
}
int main()
{
    vector <string>v;
    string s;
    int i, j;
    cout<<"Enter any string: ";
    cin>>s;
    v.push_back(s);
    print(v);   //Display contents of the container
    while(!v.empty())   //loop till container is empty
    {
        s = v[0];   //Always replace 1st string
        v.erase(v.begin()); //remove 1st string
        if(s.length()>1)    //check whether the string is of length greater than 1
        {
            s = s.substr(1);
            v.push_back(s);
            v.push_back(s);
        }
        print(v);   //Display contents of the container
    }
}

Below is the screenshot of output

CAUsers Prakher 1Documentslcltest.exe Enter any string: Hydra Hydra ydra ydra ydra dra dra dra dra dra dra dra dra dra ra r

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.

Add a comment
Know the answer?
Add Answer to:
c++ code In mythology, the Hydra was a monster with many heads. Every time the hero chopped off a head,...
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
  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • %%%%Python Question%%% Work from the template acrostic.py, which you can find on the ELMS page for...

    %%%%Python Question%%% Work from the template acrostic.py, which you can find on the ELMS page for this assignment. • In the Generator class, write an __init__() method with two parameters: self and the path to a text file containing one word per line. This method should read the words from the file, strip off leading and trailing whitespace, and store them in a dictionary where each key is a lower-case letter and each corresponding value is a list of words...

  • Recursion and Trees Application – Building a Word Index Make sure you have read and understood...

    Recursion and Trees Application – Building a Word Index Make sure you have read and understood ·         lesson modules week 10 and 11 ·         chapters 9 and 10 of our text ·         module - Lab Homework Requirements before submitting this assignment. Hand in only one program, please. Background: In many applications, the composition of a collection of data items changes over time. Not only are new data items added and existing ones removed, but data items may be duplicated. A list data structure...

  • Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And...

    Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And also I have codes but just donot work so make sure that it works. Requested files: CrosswordGenerator.cpp, CrosswordGenerator.h, CrosswordGenerator_test.cpp CrosswordGenerator - Write a program that helps to generate a crossword puzzle by organizing words that share letters.   For this assignment, you will write a program that forms the basis of a crossword puzzle generator. In order to create a crossword puzzle you need to...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective...

    SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective To gain experience with the operations involving binary search trees. This data structure as linked list uses dynamic memory allocation to grow as the size of the data set grows. Unlike linked lists, a binary search tree is very fast to insert, delete and search. Project Description When an author produce an index for his or her book, the first step in this process...

  • You will be building a linked list. Make sure to keep track of both the head and tail nodes.

    Ch 8 Program: Playlist (Java)You will be building a linked list. Make sure to keep track of both the head and tail nodes.(1) Create two files to submit.SongEntry.java - Class declarationPlaylist.java - Contains main() methodBuild the SongEntry class per the following specifications. Note: Some methods can initially be method stubs (empty methods), to be completed in later steps.Private fieldsString uniqueID - Initialized to "none" in default constructorstring songName - Initialized to "none" in default constructorstring artistName - Initialized to "none"...

  • This project is meant to give you experience writing linked lists and graphs. As such, you...

    This project is meant to give you experience writing linked lists and graphs. As such, you are not permitted to use arrays or any data structure library. You may, however, make use of code presented in class and posted to Blackboard. Objective Your goal for this project is to take a block of text, analyze it, and produce random sentences in the style of the original text. For example, your program, given Wizard of Oz, might produce: how quite lion...

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