Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

is this code correct? Modify your program created in the previous question so that it keeps lists of intermediate words during the two sorts instead

is this code correct?
"Modify your program created in the previous question so that it keeps lists of intermediate words during the two sorts instead of keeping lists of swap indices."
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// Function to check if two words are anagrams
bool areAnagrams(const string& word1, const string& word2){
if (word1.length()!= word2.length()) return false;
string sorted1= word1;
string sorted2= word2;
sort(sorted1.begin(), sorted1.end());
sort(sorted2.begin(), sorted2.end());
return sorted1== sorted2;
}
// Function to check if one word can be obtained by swapping adjacent letters
bool canTransformBySwapping(const string& word1, const string& word2){
if (word1.length()!= word2.length()) return false;
for (size_t i =0; i < word1.length()-1; ++i){
string temp = word1;
swap(temp[i], temp[i +1]);
if (temp == word2) return true;
}
return false;
}
// Function to produce a proof sequence using BFS
vector produceProofSequence(const string& start, const string& target){
if (start == target) return {start};
unordered_set visited;
queue>> q;
q.push({start,{start}});
visited.insert(start);
while (!q.empty()){
auto [current, sequence]= q.front();
q.pop();
for (size_t i =0; i < current.length()-1; ++i){
string next = current;
swap(next[i], next[i +1]);
if (visited.find(next)== visited.end()){
vector nextSequence = sequence;
nextSequence.push_back(next);
if (next == target) return nextSequence;
q.push({next, nextSequence});
visited.insert(next);
}
}
}
return {}; // return empty vector if no sequence is found
}
int main(){
try {
string word1, word2;
cout << "Enter the first word: ";
if (!(cin >> word1)){
throw invalid_argument("Invalid input for the first word.");
}
cout << "Enter the second word: ";
if (!(cin >> word2)){
throw invalid_argument("Invalid input for the second word.");
}
if (!areAnagrams(word1, word2)){
cout << "The words are not anagrams. Transformation is not possible.
";
} else {
vector proofSequence = produceProofSequence(word1, word2);
if (!proofSequence.empty()){
cout << "Transformation is possible. Proof sequence:
";
for (const string& word : proofSequence){
cout << word <<"";
}
cout <<"
";
} else {
cout << "Transformation is not possible by swapping adjacent letters.
";
}
}
} catch (const invalid_argument& e){
cerr << "Error: "<< e.what()<<"
";
} catch (const out_of_range& e){
cerr << "Error: "<< e.what()<<"
";
} catch (const exception& e){
cerr <<"An unexpected error occurred: "<< e.what()<<"
";
} catch (...){
cerr <<"An unknown error occurred.
";
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

What are loan discount fees? And what are 2 examples of it?

Answered: 1 week ago