码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1731 Orders

时间:2014-10-12 03:47:57      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   strong   

题目链接:http://poj.org/problem?id=1731

 

思路:

    含有重复元素的全排列问题;元素个数为200个,采用暴力枚举法。

代码:

 

#include <iostream>
#include <algorithm>
using namespace std;

const int MAX_N = 200 + 10;
void PrintPermu( int n, char P[], char A[], int cur )
{
    int i, j;

    if ( cur == n )
    {
        for ( i = 0; i < n; ++i )
            cout << A[i];
        cout << endl;
    }
    else
    {
        for ( int i = 0; i < n; ++i )
        {
            if ( i == 0 || P[i] != P[i-1] )
            {
                int c1 = 0, c2 = 0;

                for ( j = 0; j < cur; ++j )
                    if ( A[j] == P[i] ) c1++;
                for ( j = 0; j < n; ++j )
                    if ( P[i] == P[j] ) c2++;

                if ( c1 < c2 )
                {
                    A[cur] = P[i];
                    PrintPermu( n, P, A, cur + 1 );
                }
            }
        }
    }
}

int main()
{
    char P[MAX_N], A[MAX_N];

    cin >> P;
    sort( P, P + strlen(P) );
    PrintPermu( strlen(P), P, A, 0 );
    return 0;
}

 

poj 1731 Orders

标签:style   blog   http   color   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/tallisHe/p/4020033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!