关闭

print all permutation of a string(recursion)

标签: recursion
234人阅读 评论(0) 收藏 举报
分类:

Input a string, please print all permutation of it. For example, input”abs”,the output should be”abs”, “asb”, “bas”,”bsa”,”cas”,”csa”.

The recursion version:

#include<iostream>
using namespace std;
void CalAllPermutation(char *perm, int from, int to)
{
    if (to <= 1)
    {
        return;
    }
    if (from == to)
    {
        for (int i = 0; i <= to; i++)
        {
            cout << perm[i];
        }
        cout << endl;
    }
    else
    {
        for (int j = from; j <= to; j++)
        {
            std::swap(perm[j], perm[from]);
            CalAllPermutation(perm, from + 1, to);
            std::swap(perm[j], perm[from]);
        }
    }
}

int main()
{
    char arr[] = "abcde";
    CalAllPermutation(arr, 0, 4);
    return 0;
}
0
0
查看评论

POJ 1579 Function Run Fun 记忆化搜索

Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a 1 if a > 20 or b > 2...
  • AXuan_K
  • AXuan_K
  • 2014-08-27 15:06
  • 817

POJ 题目1579 Function Run Fun(记忆搜索)

Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14815 Accepted: 7659 Description We all love...
  • u011282069
  • u011282069
  • 2013-08-09 09:56
  • 802

hdu1579 Function Run Fun(深搜+记忆化)

hdu1579 Function Run Fun(深搜+记忆化)
  • u012860063
  • u012860063
  • 2014-07-05 20:11
  • 1596

Permutation递归解法

permutation类型题的解法
  • a6219221
  • a6219221
  • 2016-09-07 03:21
  • 522

find all the permutation of a string

举个例子, 一个数组存放数据: 1, 2, 3, 4,, 那么这个数的
  • a130737
  • a130737
  • 2014-10-20 16:31
  • 384

[LintCode] 字符串置换 String Permutation

给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换。 置换的意思是,通过改变顺序可以使得两个字符串相等。样例 “abc” 为 “cba” 的置换。 “aabc” 不是 “abcc” 的置换。Given two strings, write a method to dec...
  • wutingyehe
  • wutingyehe
  • 2016-04-21 21:03
  • 2394

【HDU】1331 - Function Run Fun(记忆化递归)

题目链接:点击打开题目 Function Run Fun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm...
  • wyg1997
  • wyg1997
  • 2016-10-23 15:29
  • 558

HDU1331 Function Run Fun

Function Run Fun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3754...
  • Puyar_
  • Puyar_
  • 2016-05-27 22:50
  • 230

Function Run Fun(递归转递推)

Link:http://acm.hdu.edu.cn/showproblem.php?pid=1579 Problem: Function Run Fun Time Limit: 1000/1000 MS (Java/Others)    Mem...
  • Enjoying_Science
  • Enjoying_Science
  • 2014-11-01 10:56
  • 636

[leetcode] 567. Permutation in String

567. Permutation in StringGiven two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of th...
  • juanqinyang
  • juanqinyang
  • 2017-05-07 20:52
  • 1105
    个人资料
    • 访问:113124次
    • 积分:3390
    • 等级:
    • 排名:第11790名
    • 原创:223篇
    • 转载:15篇
    • 译文:2篇
    • 评论:19条
    最新评论