字符串全排列问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
tsuibin
V2EX    程序员

字符串全排列问题

  •  
  •   tsuibin
    tsuibin 2011-10-25 01:17:53 +08:00 5401 次点击
    这是一个创建于 5160 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假设有字符串abc,那么写段程序打印出abc这三个字符组成的所有排列方式
    比如:
    abc
    acb
    bca
    bac
    cab
    cba
    如果是baa,则所有的排列组合应该是
    baa
    aba
    aab
    这就要考虑到一个重复字符的问题
    第一个问题已经解决了,但是第二个问题今天弄了一天也没想出好办法

    最后通过计算组合的hash,通过查询hash表的方式临时解决了
    C++ 源码见连接 http://hi.baidu.com/tsuibin/blog/item/e9a044efe9692be7b2fb953e.html

    不知道有没有人知道更好的方法
    7 条回复    1970-01-01 08:00:00 +08:00
    dreampuf
        1
    dreampuf  
       2011-10-25 01:57:23 +08:00
    单字符数组的笛卡尔积.

    strs = "abc"

    strs_args = strs * len(strs)
    for i in itertools.product(*strs_args):
    print "".join(i)
    qiao
        2
    qiao  
       2011-10-25 02:34:47 +08:00
    好久没写 C++ 了:

    <pre>
    #include <iostream>
    #include <string>
    #include <algorithm>

    using namespace std;

    template <typename T>
    void permutation(T t, void (*fn)(const T &)) {
    sort(t.begin(), t.end());
    do {
    fn(t);
    } while(next_permutation(t.begin(), t.end()));
    }

    void print(const string &s) {
    cout << s << endl;
    }

    int main() {
    string s("baa");
    permutation(s, print);
    return 0;
    }
    </pre>
    tsuibin
        3
    tsuibin  
    OP
       2011-10-25 16:16:30 +08:00
    @qiao
    这个问题的关键 就是要自己实现 next_permutation()这个函数啊
    qiao
        4
    qiao  
       2011-10-25 21:24:16 +08:00
    @tsuibin

    每次记录一下被交换的元素即可:

    #include <iostream>
    #include <string>
    #include <algorithm>

    using namespace std;

    void permutation_helper(string &str, int start, int end) {
    if (start == end) {
    cout << str << endl;
    } else {
    bool visited[256];
    fill(visited, visited + 256, false);
    for (int i = start; i <= end; ++i) {
    if (!visited[str[i]]) {
    swap(str[start], str[i]);
    permutation_helper(str, start + 1, end);
    swap(str[start], str[i]);
    visited[str[i]] = true;
    }
    }
    }
    }

    void permutation(string str) {
    sort(str.begin(), str.end());
    permutation_helper(str, 0, str.length() - 1);
    }

    int main() {
    string s("aab");
    permutation(s);
    return 0;
    }
    tsuibin
        5
    tsuibin  
    OP
       2011-10-26 19:49:42 +08:00
    @qiao
    great!
    这个方法比 我在STL源码刨析 (第380页) 看的next_permutation的实现源码更加简洁!
    gfcheng
        6
    gfcheng  
       2011-11-03 20:57:33 +08:00
    @tsuibin
    多加个数组b做参数,保存结果(全排列),可以吗?
    permutation(a, start, end,b):
    if start == end:
    c = "".join(a)
    if c not in b:
    b.append(c)
    print c
    tsuibin
        7
    tsuibin  
    OP
       2012-01-18 12:12:13 +08:00
    @gfcheng 这样的话,如果数据中有重复的,你就不会将它加入到b了,但是需要考虑字符串中有重复的情况
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5193 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 07:23 PVG 15:23 LAX 23:23 JFK 02:23
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86