Anagram POJ - 1256(字符串 + 全排列 + 编码)

题目大概:

给定字符集(可存在重复字符),按“定义字典序LEX”输出所有排列
LEX定义如下:‘A’<‘a’<‘B’<‘b’<…<‘Z’<‘z’

链接: https://vjudge.net/problem/POJ-1256

思路:

乍一看就是摆弄字符串,全排列就可以解决,不过需多一步编码

类型 A B…Z a b…z
编码 (ch - ‘A’) * 2 (ch - ‘a’) * 2 + 1
解码 x / 2 + ‘A’ x / 2 + ‘a’

实现代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include<iomanip>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
const int maxn = 1e4+10;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
//int dz[] = {0, 0, 0, 0, 1,-1};
//int dx[] = {1,-1, 0, 0, 0, 0};
//int dy[] = {0, 0, 1,-1, 0, 0};
//int dx[] = {1,-1, 0,0};
//int dy[] = {0, 0,-1,1};
//int dx[] = {-1, 1,-2, 2,-2, 2,-1, 1};
//int dy[] = {-2,-2,-1,-1, 1, 1, 2, 2};
//struct P {
//    int z, x, y;
//    P(int z = 0, int x = 0, int y = 0): z(z), x(x), y(y) {}
//};
//struct P {
//    int x, y;
//    P(int x = 0, int y = 0): x(x), y(y) {}
//};
//int r, c;
//P pre[30][30];
//int vis[30][30];
//inline bool in_border (int mx, int my) {
//    return mx>=0 && mx<r && my>=0 && my<c;
//}

int main() {int T; cin >> T; getchar();
    char s[20], a[20];
    while(T--) {
        scanf("%s", s);
        int len = strlen(s);
        for(int i = 0; i < len; i++) {
            if('A' <= s[i] && s[i] <= 'Z')
                 a[i] = (s[i] - 'A') * 2;
            else a[i] = (s[i] - 'a') * 2 + 1;
        }
        sort(a, a+len);
//                                    for(int i = 0; i < len; i++)
//                                        printf("%d ", a[i]); printf("\n");

        do {
            for(int i = 0; i < len; i++) {

                if(a[i] % 2) printf("%c", a[i] / 2 + 'a');
                else printf("%c", a[i] / 2 + 'A');
            }
            printf("\n");
        }while(next_permutation(a, a+len));
    }
    return 0;
}
发布了54 篇原创文章 · 获赞 43 · 访问量 1948

猜你喜欢

转载自blog.csdn.net/Jungle_st/article/details/104692729