[TOC]

hankin练习4

include

include

include

include

include

define maxn 105

using namespace std;

string str; set ans; stack st; char tmp[maxn]; int len;

void dfs(int index, int pos, bool isadd) { cout << 22222 << endl; if (st.empty()) { if (len <= index) { ans.insert(tmp); cout << tmp << endl; return; } } cout << 111111111 << endl; if (len <= pos) return; if (index >= len) return;

if (isadd) {
    st.push(str[pos]);
}
if (st.empty()) return;

dfs(index, pos + 1, true);
char c = st.top();
tmp[index] = c;
st.pop();
dfs(index + 1, pos, false);
dfs(index + 1, pos + 1, true);   // 不出栈加、(不出栈不加)、出栈加、出栈不加
return;

}

int main() { cin >> str; len = str.size(); memset(tmp, 0, sizeof(tmp)); dfs(0, 0, true); for (set::iterator it = ans.begin(); it != ans.end(); it++) { cout << *it << endl; } system("pause"); return 0; }

根本跑不通,段错误,可以练习gdb

下面的不知道错在哪里。。。。。

#include <iostream>
#include <string.h>
#include <string>
#include <set>
#include <stack>
#define maxn 105
using namespace std;

string str;
set<string> ans;
int len;

void dfs(int i, string st, string tmp)
{
    if (i == len) {
        reverse(st.begin(), st.end());
        ans.insert(tmp.append(st));
        return ;
    }
    st.push_back(str[i]);
    dfs(i + 1, st, tmp);
    for (; !st.empty(); ) {
        tmp.push_back(st.back());
        st.pop_back();
        dfs(i + 1, st, tmp);
    }
    return;
}

int main()
{
    while(cin >> str) {
        len = str.size();
        dfs(0, "", "");
        for (set<string>::iterator it = ans.begin(); it != ans.end(); it++) {
            cout << *it << endl;
        }
        ans.clear();
    }
    //system("pause");
    return 0;
}

results matching ""

    No results matching ""