0

Can someone help generating anagram using stacks and queue ?

void anagram(string word, stack<char> s1, stack<string> s2, Queue<char> queue){ char pivot, yo; string x="", w; int number = 1, factorial = 1, n = 1; w = word; while(number<=w.length()){ factorial *= number; ++number; } do{ w = word; for(int i=0; i<w.length(); i++){ s1.push(w[i]); } queue.enqueue(s1.top()); s1.pop(); while(s1.top() > queue.behind()){ queue.enqueue(s1.top()); s1.pop(); } if(s1.top() < queue.behind()){ pivot = s1.top(); s1.pop(); } while(queue.front() < pivot){ yo = queue.front(); queue.dequeue(); queue.enqueue(yo); } if(queue.front() > pivot){ s1.push(queue.front()); queue.dequeue(); } queue.enqueue(pivot); while(queue.front() > pivot){ yo = queue.front(); queue.dequeue(); queue.enqueue(yo); } if(queue.front() <= pivot){ s1.push(queue.front()); queue.dequeue(); } while(!queue.IsEmpty()){ s1.push(queue.front()); queue.dequeue(); } while(!s1.IsEmptyStack()){ x = x + s1.top(); s1.pop(); } cout << x << endl; s2.push(x); word = x; x.clear(); ++n; }while(n<=factorial); } How do we not duplicate the word ? This is the output: Input : ali lia ail ila ial ali lia And this output is not printing lia which is one of the anagrams

28th May 2020, 8:51 AM
Nadhirah
0 Respuestas