ZOJ1004 Anagrams by Stack

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ1004 Anagrams by Stack相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/using' title='using'>using   ram   void   鐜板湪   nbsp   code   space   鍑烘爤   col   

棰樼洰澶ф剰锛氳瀹?i 涓哄叆鏍堬紝o 涓哄嚭鏍堬紝鐜板湪缁欎袱涓瓧绗︿覆st1,st2锛岀幇鍦ㄨ灏唖t1杞寲涓簊t2锛岃浆鍖栨柟娉曟槸锛宻t1涓瓧绗︿粠澶村紑濮嬪叆鏍堬紝骞跺悎鐞嗗嚭鏍堟瀯閫犲嚭st2銆傝杈撳嚭鎵€鏈夊彲鑳界殑鍑哄叆鏍堟楠ゃ€?/p>

娣卞害浼樺厛鎼滅储+鍥炴函~

#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int len;
stack<char> st;
vector<char> path;
void dfs (int ipush,int ipop) {
    if (ipush==len&&ipop==len) {
        for (int i=0;i<path.size();i++)
        printf ("%c ",path[i]);
        printf ("
");
        return;
    }
    if (ipush+1<=len) {
        st.push(s1[ipush]);
        path.push_back(鈥?/span>i鈥?/span>);
        dfs(ipush+1,ipop);
        st.pop();
        path.pop_back();
    }
    if (ipop+1<=len&&!st.empty()&&st.top()==s2[ipop]) {
        char now=st.top();
        st.pop();
        path.push_back(鈥?/span>o鈥?/span>);
        dfs(ipush,ipop+1);
        st.push(now);
        path.pop_back();
    }
} 
int main () {
    while (cin>>s1>>s2) {
        len=s1.length();
        printf ("[
");
        dfs(0,0);
        printf ("]
");
    }
    return 0;
}

 

以上是关于ZOJ1004 Anagrams by Stack的主要内容,如果未能解决你的问题,请参考以下文章

ZOJ 1004 Anagrams by Stack

12Anagrams by Stack

Anagrams by Stack(STL)

Anagrams by Stack(STL)

Anagrams by Stack(STL)

Java - 迭代 ArrayList 并查找 Anagrams