I am trying to solve Uva 732: Anagrams by Stack for last two days and it passes all the test cases from uDebug(and also custom input). And I am not able to get AC verdict.
Question link : link
#include<bits/stdc++.h>
using namespace std;
void permute(vector<vector<char>> &answers, vector<char> curStr, const string &s1, int is1, const string &s2, int is2,stack<char> stk){
if(is1 == is2 && is1 == s1.length()){
//finished
//cout<<"Never Reached\n";
answers.push_back(curStr);
return;
}
if(stk.size() == 0){
curStr.push_back('i');
stk.push(s1[is1++]);
permute(answers,curStr,s1, is1, s2, is2, stk);
}
else{
if(is1 < s1.length()){
curStr.push_back('i');
stk.push(s1[is1++]);
permute(answers,curStr,s1, is1, s2, is2, stk);
stk.pop();
is1--;
curStr.pop_back();
}
if(stk.top() == s2[is2]){
curStr.push_back('o');
stk.pop();
is2++;
permute(answers,curStr,s1, is1, s2, is2, stk);
// is2--;
// stk.push(s2[is2]);
// curStr.pop_back();
}
}
}
int main(){
string s1,s2;
while(cin>>s1>>s2){
cout<<"[\n";
vector<vector<char>> answers;
stack<char> stk;
vector<char> curStr;
permute(answers, curStr, s1, 0, s2, 0, stk);
for(int i = 0; i < answers.size(); i++){
for(int j = 0; j < answers[i].size(); j++){
cout<<answers[i][j];
if(j != answers[i].size() - 1){
cout<<" ";
}
}
cout<<"\n";
}
cout<<"]\n";
}
}