poj 1256 Anagram

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

思路:

    该题为含有重复元素的全排列问题;由于题目中字符长度较小,采用暴力法解决。

代码如下:

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

const int MAX_N = 20;
char P[MAX_N], A[MAX_N];

char * SortAlp( char P[], int n )
{
    int Low[MAX_N], Upper[MAX_N];
    int LowLen, UpperLen;

    LowLen = UpperLen = 0;
    for ( int i = 0; i < n; ++i )
    {
        if ( ‘A‘ <= P[i] && P[i] <= ‘Z‘ )
            Upper[UpperLen++] = P[i];
        else
            Low[LowLen++] = P[i];
    }

    sort( Low, Low + LowLen );
    sort( Upper, Upper + UpperLen );

    int Index_L, Index_U;
    Index_L = Index_U = 0;
    for ( int j = 0; j < n; ++j )
    {
        if (Upper[Index_U] - ‘A‘ + ‘a‘ <= Low[Index_L]
            && Index_U < UpperLen)
            P[j] = Upper[Index_U++];
        else
            P[j] = Low[Index_L++];
    }

    return P;
}

void PrintPermutation( int n, char P[], char A[], int cur )
{
    int i, j;

    if ( cur == n )
    {
        for ( i = 0; i < n; ++i )
            printf( "%c", A[i] );
        printf("\n");
    }
    else
    {
        for ( i = 0; i < n; ++i )
        {
            if ( !i || 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];
                    PrintPermutation( n, P, A, cur + 1 );
                }
            }
        }
    }
}

int main()
{
    int n;
    char P[MAX_N];

    cin >> n;

    for ( int i = 0; i < n; ++i )
    {
        cin >> P;
        SortAlp( P, strlen(P) );
        PrintPermutation( strlen(P), P, A, 0 );
    }

    return 0;
}
时间: 2024-12-13 10:32:31

poj 1256 Anagram的相关文章

7 POJ 1256 Anagram

给一个字符串包含大小写字符,规定'A'<'a'<'B'<'b'<...<'Z'<'z',求该字符串的全排列. 用裸的dfs+map判重 写了一遍超时了,那种机智的dfs方法没有怎么看懂.. 最开始用的set+next_permutation,太年轻,也超时了... 运用一个next_permutation()函数即可,<algorithm>头文件 注意要先将字符串sort一遍,然后next_permutation()也要把比较函数cmp传进去,原来都不知道可

POJ 1256 Anagram(输入可重集枚举排序)

[题意简述]:本题题意很好理解!题目给出的Hint,使我们对关键点有了更加清晰的认识 An upper case letter goes before the corresponding lower case letter. So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'. 就是给一个序列(序列可以有重复的元素),让我们输出它的所有排列,字母顺序规定给出! [分析]:这道题是我之前学习枚举排序和子

poj 2408 Anagram Groups(hash)

题目链接:poj 2408 Anagram Groups 题目大意:给定若干个字符串,将其分组,按照组成元素相同为一组,输出数量最多的前5组,每组按照字典序输出所 有字符串.数量相同的输出字典序较小的一组. 解题思路:将所有的字符串统计字符后hash,排序之后确定每组的个数并且确定一组中字典序最小的字符串.根据个数 以及字符串对组进行排序. #include <cstdio> #include <cstring> #include <vector> #include &

[2016-01-19][POJ][1256]

时间:2016-01-19  13:41:17  星期二 题目编号:POJ 1256 题目大意:给出一个word,求word组成字母的全排列,并输出 要求 输出顺序按字母顺序 字母顺序为 'A'<'a'<'B'<'b'<...<'Z'<'z' 方法: 通过上面的字母顺序构造一个cmp函数 先对原来的word进行重新排序 然后 用 next_permutation 进行生成后续排列,边生成边输出即可 解题过程遇到问题: next_permutation() 有第三个参数 

POJ 1256:Anagram

Anagram Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18393 Accepted: 7484 Description You are to write a program that has to generate all possible words from a given set of letters. Example: Given the word "abc", your program shoul

next_permutation,POJ(1256)

题目链接:http://poj.org/problem?id=1256 解题报告: 1.sort函数是按照ASC11码排序,而这里是按照 'A'<'a'<'B'<'b'<...<'Z'<'z'排序. #include <iostream> #include <algorithm> #include <string> using namespace std; bool cmp(char a,char b) { char m=tolowe

POJ 2408 - Anagram Groups - [字典树]

题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new application for his theory on the distribution of characters in English language texts. Given such a text

POJ 2408 Anagram Groups 排序到极致

Anagram Groups Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4903   Accepted: 1316 Description World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new application for his theory on t

poj 2408 Anagram Groups

Description World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new application for his theory on the distribution of characters in English language texts. Given such a text, you are to find the la