We are given two strings, called the source and the target , which both have the
ID: 3569365 • Letter: W
Question
We are given two strings, called the source and the target, which both have the same number of characters n. The goal is to transform the target string into the source string using the smallest number of allowed operations. There are two types of allowed operations, a flip and a substitution.
A substitution replaces a character in a certain position by another character. For example eetie is transformed to eerie via a substitution of the third character.
A flip reverses a contiguous substring of the string. In particular, flip(i,j) reverses the substring starting at the i-th character and ending at the j-th character. For example, eteie is transformed to eetie via flip(2,3).
We should assume that the portions where any two flips are applied don't overlap. That is, if we perform flip(i,j) and flip(k,l), then the sets {i,i+1,...,j} and {k,k+1,...,l} are disjoint.
Consider the source string timeflieslikeanarrow and the target string tfemiliilzejeworrbna. The following sequence transforms the target into the source:
tfemiliilzejeworrbna (the target)
tfemiliilzejeanbrrow (a flip)
tfemiliezlijeanbrrow (a flip)
timefliezlijeanbrrow (a flip)
timefliezlijeanarrow (a substitution)
timefliezlikeanarrow (a substitution)
timeflieslikeanarrow (a substitution)
The number of operations used, which is the quantity we seek to minimize, was 6. Note that we never need more operations than the length of the given strings.
Write a time-efficient program that takes as input a file containing the two input strings and prints out a minimum length sequence of transformations from the target to the source. You can assume that the file is formatted like this:
Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
typedef enum
{
VALUE,
JACK,
QUEEN,
KING,
ACE
} FACE;
typedef struct
{
int value;
FACE f;
} CARD;
void print_card(CARD c)
{
if (c.f == ACE)
cout << "ACE (11)";
else if (c.f == KING)
cout << "KING (10)";
else if (c.f == QUEEN)
cout << "QUEEN (10)";
else if (c.f == JACK)
cout << "JACK (10)";
else if (c.f == VALUE)
{
if (c.value == 10)
cout << "TEN (10)";
else if (c.value == 9)
cout << "NINE (9)";
else if (c.value == 8)
cout << "EIGHT (8)";
else if (c.value == 7)
cout << "SEVEN (7)";
else if (c.value == 6)
cout << "SIX (6)";
else if (c.value == 5)
cout << "FIVE (5)";
else if (c.value == 4)
cout << "FOUR (4)";
else if (c.value == 3)
cout << "THREE (3)";
else if (c.value == 2)
cout << "TWO (2)";
else
cout << "Value not recognized";
}
else
cout << "Face not recognized";
}
void init_deck(CARD deck[])
{
for (int index = 0; index < 4; index++)
{
int seg = index * 13;
deck[seg + 0].f = VALUE;
deck[seg + 0].value = 2;
deck[seg + 1].f = VALUE;
deck[seg + 1].value = 3;
deck[seg + 2].f = VALUE;
deck[seg + 2].value = 4;
deck[seg + 3].f = VALUE;
deck[seg + 3].value = 5;
deck[seg + 4].f = VALUE;
deck[seg + 4].value = 6;
deck[seg + 5].f = VALUE;
deck[seg + 5].value = 7;
deck[seg + 6].f = VALUE;
deck[seg + 6].value = 8;
deck[seg + 7].f = VALUE;
deck[seg + 7].value = 9;
deck[seg + 8].f = VALUE;
deck[seg + 8].value = 10;
deck[seg + 9].f = JACK;
deck[seg + 9].value = 10;
deck[seg + 10].f = QUEEN;
deck[seg + 10].value = 10;
deck[seg + 11].f = KING;
deck[seg + 11].value = 10;
deck[seg + 12].f = ACE;
deck[seg + 12].value = 11;
}
}
void print_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
print_card(deck[x]);
cout << endl;
}
}
void shuffle_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
int new_index = rand() % 52;
CARD temp = deck[x];
deck[x] = deck[new_index];
deck[new_index] = temp;
}
}
int main()
{
srand();
CARD deck[52];
init_deck(deck);
shuffle_deck(deck);
print_deck(deck);
int bank;
int seed;
int bet;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Enter bet:" << endl;
cin >> bet;
cout << "Current hand: " << srand(seed) % 3 << ".";
cout << "Player has: " << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.