Question

In: Computer Science

C++: A string construction problem defined as follows: - You are given as input a target...

C++: A string construction problem defined as follows:

- You are given as input a target string

- Starting with an empty string, you add characters to it, until your new string is same as the target. You have two options to add characters to a string:

- You can append an arbitrary character to your new string, with cost x

- You can clone any substring of your new string so far, and append it to the end of your new string, with cost y

- For a given target, append cost x, and clone cost y, we want to know what the *cheapest cost* is of building the target string

For some simple examples:

- Target "aa", append cost 1, clone cost 2: the cheapest cost is 2:

- Start with an empty string, ""

- Append 'a' (cost 1), giving the string "a"

- Append 'a' (cost 1), giving the string "aa"

- Target "aaaa", append cost 2, clone cost 3: the cheapest cost is 7:

- Start with an empty string, ""

- Append 'a' (cost 2), giving the string "a"

- Append 'a' (cost 2), giving the string "aa"

- Clone "aa" (cost 3), giving the string "aaaa"

- Target "xzxpzxzxpq", append cost 10, clone cost 11: the cheapest cost is 71:

- Start with an empty string, ""

- Append 'x' (cost 10): "x"

- Append 'z' (cost 10): "xz"

- Append 'x' (cost 10): "xzx"

- Append 'p' (cost 10): "xzxp"

- Append 'z' (cost 10): "xzxpz"

- Clone "xzxp" (cost 11): "xzxpzxzxp"

- Append 'q' (cost 10) : "xzxpzxzxpq"

In the file `StringConstruction.h` implement a function `stringConstruction` that takes the target string, the clone cost, and the append cost (in that order), and returns the cheapest way of making the target string. It doesn't need to return *how* to do it, just the cost.

To test your code, TestStringCons.cpp contains some test cases.

Note: it will need to work for the largest of these target strings, which is 30,000 characters. the code will be ran on a modest desktop PC, and allowed 2 seconds.

StringConstruction.h file:

#ifndef STRINGCONSTRUCTION_H

#define STRINGCONSTRUCTION_H

#include <string>

using std::string;

// TODO: your code goes here:

// do not write or edit anything below this line

#endif

TestStringCons.cpp file:

#include "StringConstruction.h"

#include <iostream>

using std::cout;

using std::endl;

using std::ostream;

#include <vector>

using std::vector;

struct Testcase {

string target;

int appendCost;

int cloneCost;

int bestCost;

string notes;

};

ostream & operator<<(ostream & o, const Testcase & t) {

o << "Target string: " << t.target << "\n";

o << "Cost of appending an arbitrary character: " << t.appendCost << "\n";

o << "Cost of cloning a substring, and appending: " << t.cloneCost << "\n";

o << "Cheapest way to make the string should be: " << t.bestCost << "\n";

return o;

}

int main() {

int retval = 0;

vector<Testcase> testcases{

{"aa",1,2,2," Append 'a' (cost 1), append 'a' (cost 1)"},

{"aaaa",2,3,7," Append 'a' (cost 2), append 'a' (cost 2), clone 'aa' (cost 3)"},

{"xzxpzxzxpq", 10, 11, 71, " Append 'x' (cost 10), Append 'z' (cost 10), Append 'x' (cost 10), Append 'p' (cost 10), Append 'z' (cost 10),\n Clone 'xzxp' (cost 11), Append 'q' (cost 10)"}

};

for (size_t i = 0; i < testcases.size(); ++i) {

cout << "Testcase " << i << "\n";

cout << testcases[i];

cout << "\nRunning your function\n";

int costWas = stringConstruction(testcases[i].target, testcases[i].cloneCost, testcases[i].appendCost);

if (costWas == testcases[i].bestCost) {

cout << " -- Test passed, correct cost was returned\n\n\n";

} else {

cout << " -- Test failed, incorrect cost of " << costWas << " was returned\n";

cout << " To help your debugging, the cheapest way to make the string should be:\n";

cout << testcases[i].notes << "\n\n\n";

++retval;

}

}

string bigString = "abcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifpblgmbtmblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifpblgmbtmblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcqaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifpblgmbtmblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjoirmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifpblgmbtmblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipiblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifpblgmbtmblgmbaipfdmbcntdblgblgmbaipmbcntdblgblgmbaipcbcntdblgblgmbaipobacjodblgblgmbaiabcblgmbcntdblgblgmbaipmbcntdblgblgmbaipfdmbcqaipfdmbcntdblgblgmbaipmbcntdblgblgmbaiprtifbcntdblgblgmbaipmbcntdblgblgmbaip";

string doubleIt = bigString + bigString;

cout << "Big target string testcase, using a " << doubleIt.size() << " character test string. An excellent solution will complete in around a hundredth of a second.\n";

cout << "(Note -- if the program freezes at this point, Ctrl+C will stop it from running)\n";

int result = stringConstruction(doubleIt, 1235, 1234);

if (result == 59249) {

cout << "-- Correct cost of 59249 returned\n";

} else {

cout << "-- Incorrect cost of " << result << " returned, rather than 59249\n";

}

return retval;

}

Solutions

Expert Solution

#ifndef STRINGCONSTRUCTION_H

#define STRINGCONSTRUCTION_H

#include <string>
#include <iostream>


using std::string;
int stringConstruction(string target, int cloneCost, int appendCost) {
        string constructedString = "";
        int totalcost = 0;
        int lastIndex = 0;
                while (target != constructedString) {
                        int currLength = constructedString.length();
                        if (currLength == 0) {
                                totalcost += appendCost;
                                constructedString += target[0];
                                //std::cout << "append: " << target[0] << " cost: " << appendCost<<std::endl;
                                lastIndex = 1;
                        }
                        else {
                                bool cloned = false;
                                for (int i = currLength; i > 0; i--) {
                                        if (i*appendCost > cloneCost) {
                                                string sub = target.substr(lastIndex, i);
                                                if ((constructedString.find(sub, 0)) != string::npos) {
                                                        constructedString += sub;
                                                        cloned = true;
                                                        totalcost += cloneCost;
                                                        //std::cout << "clone: " << sub << " cost: " << cloneCost<<std::endl;
                                                        break;
                                                }
                                        }
                                }
                                if (!cloned) {
                                        constructedString += target[lastIndex];
                                        totalcost += appendCost;
                                        //std::cout << "append: " << target[lastIndex] << " cost: " << appendCost<<std::endl;
                                }
                                lastIndex = constructedString.length();
                        }
                }
        return totalcost;

}


#endif

Related Solutions

Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
In this Exercise, you have to take a single string as input. Using this input string,...
In this Exercise, you have to take a single string as input. Using this input string, you have to create multiple queues in which each queue will comprise of separate word appeared in input string. At the end, you will again concatenate all queues to a single queue.s Example: String = “Data Structure and Algo” Q1 = D → a → t → a Q2 = S → t → r → u → c → t → u →...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string you entered is: aBC_Three_DEf An output for just this specific input will be fine. If you want to provide a program for all outputs, it would help with my understanding of how the program works overall as well but it is not needed. Thanks!
The subset-sum problem is defined as follows. Given a set of n positive integers, S =...
The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1, a2, a3, ..., an} and positive integer W, is there a subset of S whose elements sum to W? Design a dynamic program to solve the problem. (Hint: uses a 2-dimensional Boolean array X, with n rows and W+1 columns, i.e., X[i, j] = 1,1 <= i <= n, 0 <= j <= W, if and only if there is a subset of...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Suppose you are given a string containing only the characters ( and ). In this problem,...
Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero...
This is for C programming. Suppose one string is defined for a sentence with maximum 100...
This is for C programming. Suppose one string is defined for a sentence with maximum 100 characters. Write a method that prints maximum occurring characters in the input string and its frequency. For example, if input string is "this is testt", your code should print 't' : 4. Thank you.
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT