天天看點

zoj1004 Anagrams by Stack

  1. //zoj1004 Anagrams by Stack
  2. //Accepted 1004 C ++ 00:00.00 860K
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <string>
  6. #include <stack>
  7. using namespace std;
  8. string s,t;
  9. string ans;
  10. int len,sp,tp,lena;
  11. void dfs(stack< char > st)
  12. {
  13.        if (tp==len){
  14.               for (int i=0; i<lena; ++i)
  15.                      cout << ans[i] << ' ';
  16.               cout << endl;
  17.               return ;
  18.        }
  19.        if (sp<len){
  20.               st.push(s[sp++]);
  21.               ans[lena++] = 'i';
  22.               dfs(st);
  23.               lena--;
  24.               sp--;
  25.               st.pop();
  26.        }
  27.        char c;
  28.        if (!st.empty() && (c=st.top())==t[tp]){
  29.               st.pop();
  30.               tp++;
  31.               ans[lena++] = 'o';
  32.               dfs(st);
  33.               lena--;
  34.               tp--;
  35.               st.push(c);
  36.        }
  37. }
  38. void solve()
  39. {
  40.        cout << '[' << endl;
  41.        if ((len=s.length())==t.length()){
  42.               sp = tp = lena = 0;
  43.               stack< char > st;
  44.               dfs(st);
  45.        }
  46.        cout << ']' << endl;
  47. }
  48. int main()
  49. {
  50. #ifdef ONLINE_JUDGE
  51. #else
  52.        freopen("1004.txt","r",stdin);
  53. #endif
  54.        while (cin >> s >> t)
  55.               solve();
  56. #ifdef ONLINE_JUDGE
  57. #else
  58.        fclose(stdin);
  59. #endif
  60.        return 0;
  61. }