[搜索] [洛谷] P1691 有重复元素的排列问题

搜索 OR next_permutation(arr, arr + len);

//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;

const int MAXN = 1e6 + 10;

int times[27] = {0}, ans = 0, len;

int arr[MAXN] = {0};

void dfs(int step)
{
    if(step == len)
    {
        ans++;

        for(int i = 0; i < len; i++)
            cout<<char(arr[i] + 'a');
        cout<<endl;
        return ;
    }
    else
    {
        for(int i = 0; i < 26; i++)
        {
            if(times[i] > 0)
            {
                arr[step] = i;
                times[i]--;
                dfs(step + 1);
                times[i]++;
            }
            
        }
    }
}

int main()
{
    //ios::sync_with_stdio(false);

    //cin.tie(0);     cout.tie(0);

    cin>>len;

    char a;

    for(int i = 0; i < len; i++)
    {
        cin>>a;
        times[a - 'a']++;
    }

    dfs(0);

    cout<<ans<<endl;

    return 0;
}

 

posted @ 2018-09-24 10:59  张浦  阅读(71)  评论(0编辑  收藏  举报