相关疑难解决方法(0)

有没有更好的方法来排列字符串?

void permute(string elems, int mid, int end)
{
    static int count;
    if (mid == end) {
        cout << ++count << " : " << elems << endl;
        return ;
    }
    else {
    for (int i = mid; i <= end; i++) {
            swap(elems, mid, i);
            permute(elems, mid + 1, end);
            swap(elems, mid, i);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的函数显示了str(str[0..mid-1]作为一个稳定的前缀和str[mid..end]一个可置换的后缀)的排列.所以我们可以permute(str, 0, str.size() - 1)用来显示一个字符串的所有排列.

但该函数使用递归算法; 也许它的表现可以改善?

是否有更好的方法来置换字符串?

c++ string algorithm permutation

50
推荐指数
4
解决办法
3万
查看次数

标签 统计

algorithm ×1

c++ ×1

permutation ×1

string ×1