stack 和 queue 的模拟题
#include<iostream> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> using namespace std ; string s1,s2 ; queue<char> a,b ; stack<char> s ; int o ; int check[1000] ; int main(){ cin >> s1 >> s2 ; for(int i = 0 ; i < s1.size() ; i ++) a.push(s1[i]) ; for(int i = 0 ; i < s2.size() ; i ++) b.push(s2[i]) ; o = 1 ; while(!a.empty() && !b.empty()){ if(o==1){ s.push(a.front()) ; a.pop() ; if(check[s.top()]){ char tmp = s.top() ; a.push(s.top()) ; s.pop() ; while( s.top()!=tmp){ a.push(s.top()) ; check[s.top()]=0; s.pop() ; } o = 1 ; a.push(tmp); check[tmp]=0; s.pop() ; }else { check[s.top()] = 1 ; o = 2 ; } } else{ s.push(b.front()) ; b.pop() ; if(check[s.top()]){ char tmp = s.top() ; b.push(tmp) ; s.pop() ; while( s.top()!=tmp){ b.push(s.top()) ; check[s.top()]=0; b.pop() ; } b.push(tmp); check[tmp]=0; s.pop() ; o = 2 ; }else { check[s.top()] = 1 ; o = 1 ; } } } while(!a.empty()){ cout << a.front() ; a.pop() ; } while(!b.empty()){ cout << b.front() ; b.pop() ; } }