代码题(50)— 字符串的排列

1、字符串排列

  输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

(1)交换元素位置

class Solution {
public:
    vector<string> Permutation(string str) {
        vector<string> res;
        if(str.empty())
            return res;
        permuteDfs(str,0,res);
        sort(res.begin(),res.end());
        return res;
        
    }
    void permuteDfs(string &str, int pos,vector<string> &res)
    {
        if(pos == str.size())
            res.push_back(str);
        else{
            for(int i=pos;i<str.size();++i)
            {
                if(i>pos && str[i] == str[pos])
                    continue;
                swap(str[i],str[pos]);
                permuteDfs(str,pos+1,res);
                swap(str[i],str[pos]);
            }
        }
    }
};

(2)数组排列中使用的方法

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include <vector>
#include<string>
#include<sstream>
#include<map>
#include<set>
#include <functional> // std::greater
using namespace std;

void permuteDfs(string &str, int pos, vector<int> &visited, string &temp, vector<string> &res)
{
    if (temp.size() == str.size())
    {
        res.push_back(temp);
        return;
    }
    for (int i = 0; i < str.size(); ++i)
    {
        if (visited[i] == 0)
        {
            if (i > 0 && str[i] == str[i - 1] && visited[i - 1] == 0)
                continue;
            visited[i] = 1;
            temp += str[i];
            permuteDfs(str, i + 1, visited, temp, res);
            int size = temp.size() - 1;
            temp = temp.substr(0,size);
            visited[i] = 0;
        }
    }
}


int main()
{
    string str;
    while (cin >> str)
    {
        set<string> result;//使用set容器去重
        vector<string> res;
        if (str.empty())
            return 0;
        string temp;
        vector<int> visited(str.size(), 0);
        permuteDfs(str, 0, visited, temp, res);

        cout << endl;
        for (int i = 0; i < res.size(); ++i)
        {
            cout << res[i] << endl;
        }
        //将 vector 容器中的内容存到 set 容器中
        result = set<string>(res.begin(), res.end());
        cout << endl;

        for (string s : result)
        {
            cout << s << endl;
        }
    }

    system("pause");
    return 0;
}

 2、字符串组合

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include <vector>
#include<string>
using namespace std;

void combineDfs(string &str, int pos, string &temp, vector<string> &res)
{
    res.push_back(temp);
    for (int i = pos; i<str.size(); ++i)
    {
        temp.push_back(str[i]);
        combineDfs(str, i + 1, temp, res);
        temp.pop_back();
    }
}
vector<string> combine(string &str)
{
    vector<string> res;
    string temp;
    combineDfs(str, 0, temp, res);
    return res;
}

int main()
{
    string str;
    cin >> str;
    vector<string> res;
    sort(str.begin(), str.end());
    res = combine(str);
    sort(res.begin(), res.end());
    for (int i = 0; i<res.size(); ++i)
    {
        cout << res[i] << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}

 

3、字符串连续的组合

输入一个字符串,输出该字符串中相邻字符的所有组合。
举个例子,如果输入abc,它的组合有a、b、c、ab、bc、abc。(注意:输出的组合需要去重)(40分)

输入描述:一个字符串
输出描述:一行,每个组合以空格分隔,相同长度的组合需要以字典序排序,且去重。
示例1:输入:bac  输出:a b c ac ba bac
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include <vector>
#include<string>
#include<sstream>
#include<map>
#include<set>
#include <functional> // std::greater
using namespace std;

void combineDfs(string &str, int pos, string &temp, vector<string> &res)
{
    res.push_back(temp);
    if (pos < str.size())//如果要求连续的字符,这里就不要循环
    {
        temp.push_back(str[pos]);
        combineDfs(str, pos + 1, temp, res);
        temp.pop_back();
    }
}
vector<string> combine(string &str)
{
    vector<string> res;
    string temp;
    for (int i = 0; i<str.size(); ++i)
    {
        temp.push_back(str[i]);
        combineDfs(str, i + 1, temp, res);
        temp.pop_back();
    }
    return res;
}
//排序的比较方式,长度相同则字母排序,否则长度排序
bool compare( string s1, string s2)
{
    if (s1.size() == s2.size())
        return s1 < s2;
    else if (s1.size() < s2.size())
        return true;
    return false;
}

int main()
{
    string str;
    cin >> str;
    vector<string> res;
    res = combine(str);
    sort(res.begin(), res.end(),compare);
    for (int i = 0; i<res.size(); ++i)
    {
        // 如果排序后的两个值相等,则只输出前者
        if (i>0 && res[i] == res[i - 1])
            continue;
        cout << res[i] << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}

 

posted @ 2018-08-21 11:08  深度机器学习  阅读(287)  评论(0编辑  收藏  举报