Question

I'm trying to write a solver that can find a single solution to a cryptarithmetic puzzle....

I'm trying to write a solver that can find a single solution to a cryptarithmetic puzzle. I need help writing the other function from this code.

/* ExhaustiveSolve

* ---------------

* This is the "not-very-smart" version of cryptarithmetic solver. It takes

* the puzzle itself (with the 3 strings for the two addends and sum) and a

* string of letters as yet unassigned. If no more letters to assign

* then we've hit a base-case, if the current letter-to-digit mapping solves

* the puzzle, we're done, otherwise we return false to trigger backtracking

* If we have letters to assign, we take the first letter from that list, and

* try assigning it the digits from 0 to 9 and then recursively working

* through solving puzzle from here. If we manage to make a good assignment

* that works, we've succeeded, else we need to unassign that choice and try

* another digit. This version is easy to write, since it uses a simple

* approach (quite similar to permutations if you think about it) but it is

* not so smart because it doesn't take into account the structure of the

* puzzle constraints (for example, once the two digits for the addends have

* been assigned, there is no reason to try anything other than the correct

* digit for the sum) yet it tries a lot of useless combos regardless

*/

bool ExhaustiveSolve(puzzleT puzzle, string lettersToAssign)

{

    if (lettersToAssign.empty()) // no more choices to make

        return PuzzleSolved(puzzle); // checks arithmetic to see if works

    for (int digit = 0; digit <= 9; digit++)   // try all digits

    {

        if (AssignLetterToDigit(lettersToAssign[0], digit))

        {

            if (ExhaustiveSolve(puzzle, lettersToAssign.substr(1)))

                return true;

            UnassignLetterFromDigit(lettersToAssign[0], digit);

        }

    }

    return false; // nothing worked, need to backtrack

}

Homework Answers

Answer #1

code:-Crytptoairthmetic Problem

int check(node* nodeArr, const int count, string s1, string s2, string s3)

{

    int val1 = 0, val2 = 0, val3 = 0, m = 1, j, i;

// calculate number corresponding to first string

    for (i = s1.length() - 1; i >= 0; i--)

    {

        char ch = s1[i];

        for (j = 0; j < count; j++)

            if (nodeArr[j].c == ch)

                break;

val1 += m * nodeArr[j].v;

m *= 10;

    }

    m = 1;

// calculate number corresponding to second string

    for (i = s2.length() - 1; i >= 0; i--)

    {

        char ch = s2[i];

        for (j = 0; j < count; j++)

            if (nodeArr[j].c == ch)

                break;

val2 += m * nodeArr[j].v;

        m *= 10;

    }

    m = 1;

// calculate number corresponding to third string

    for (i = s3.length() - 1; i >= 0; i--)

    {

        char ch = s3[i];

        for (j = 0; j < count; j++)

            if (nodeArr[j].c == ch)

                break;

  val3 += m * nodeArr[j].v;

        m *= 10;

    }

// sum of first two number equal to third return true

    if (val3 == (val1 + val2))

        return 1;

// else return false

    return 0;

}

// Recursive function to check solution for all permutations

bool permutation(const int count, node* nodeArr, int n, string s1, string s2, string s3)

{

    // Base case

    if (n == count - 1)

    {

  // check for all numbers not used yet

        for (int i = 0; i < 10; i++)

        {

// if not used

            if (use[i] == 0)

            {

// assign char at index n integer i

                nodeArr[n].v = i;

     // if solution found

                if (check(nodeArr, count, s1, s2, s3) == 1)

                {

                    cout << " Solution found: ";

                    for (int j = 0; j < count; j++)

                        cout << " " << nodeArr[j].c << " = "

                             << nodeArr[j].v;

                    return true;

                }

            }

        }

        return false;

    }

for (int i = 0; i < 10; i++)

    {

// if ith integer not used yet

        if (use[i] == 0)

        {

  // assign char at index n integer i

            nodeArr[n].v = i;

            // mark it as not available for other char

            use[i] = 1;

            // call recursive function

            if (permutation(count, nodeArr, n + 1, s1, s2, s3))

                return true;

            // backtrack for all other possible solutions

            use[i] = 0;

        }

    }

    return false;

}

bool solveCryptographic(string s1, string s2, string s3)

{

    // count to store number of unique char

    int count = 0;

    // Length of all three strings

    int l1 = s1.length();

    int l2 = s2.length();

    int l3 = s3.length();

    // vector to store frequency of each char

    vector<int> freq(26);

    for (int i = 0; i < l1; i++)

        ++freq[s1[i] - 'A'];

    for (int i = 0; i < l2; i++)

        ++freq[s2[i] - 'A'];

    for (int i = 0; i < l3; i++)

        ++freq[s3[i] - 'A'];

// count number of unique char

    for (int i = 0; i < 26; i++)

        if (freq[i] > 0)

            count++;

  // solution not possible for count greater than 10

    if (count > 10)

    {

        cout << "Invalid strings";

        return 0;

    }

// array of nodes

    node nodeArr[count];

   // store all unique char in nodeArr

    for (int i = 0, j = 0; i < 26; i++)

    {

        if (freq[i] > 0)

        {

            nodeArr[j].c = char(i + 'A');

            j++;

        }

    }

    return permutation(count, nodeArr, 0, s1, s2, s3);

}

Know the answer?
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
Similar Questions
Given a cryptarithm puzzle using four letters, such as ['a','b','cd'], and a list of the four...
Given a cryptarithm puzzle using four letters, such as ['a','b','cd'], and a list of the four solution digits, such as [1,4,6,8], figure out the mapping of digits to letters that solves the cryptarithm. There will be two addends, so the length of the puzzle list is 3. The solution should be expressed as a string; for example, '6+8=14 This is the algorithm to follow             Create a list of the four distinct letters used in the puzzle. Example: ['a','b','c','d']            ...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT