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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值