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

题目大概:

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

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

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

类型A B…Za 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值