列表

详情


OR49. 解密

描述

亮亮深吸一口气,小心地将盒子打开,里面是一张地图,地图上除了一些奇怪的字母以外没有任何路线信息,这可让亮亮犯了愁,这些字母代表了什么意思呢? 亮亮绞尽脑汁也想不出什么思路,突然,亮亮眼前一亮,“我可以把这些字母所有的排列方式全部写出来,一定可以找到答案!” 于是,亮亮兴奋的开始寻找字母里的秘密。

输入描述

每组数据输入只有一行,是一个由不同的大写字母组成的字符串,已知字符串的长度在1到9之间,我们假设对于大写字母有'A' < 'B' < ... < 'Y' < 'Z'。

输出描述

输出这个字符串的所有排列方式,每行一个排列,要求字母序比较小的排列在前面。

示例1

输入:

WHL

输出:

HLW<br/> HWL<br/> LHW<br/> LWH<br/> WHL<br/> WLH<br/>

原站题解

C++ 解法, 执行用时: 1ms, 内存消耗: 244KB, 提交时间: 2017-06-16

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//全排列的经典算法
void permulation(int step,string &str,vector<string> &ans){
    if(step == str.size()-1)
    ans.push_back(str);
 
    for (int i = step; i < str.size(); i++)
    {
        swap(str[step], str[i]);
        permulation(step+1, str, ans);
        swap(str[step], str[i]);
    }
}
int main()
{
    string str;
    while(cin>>str)
    {
        vector<string> ans;
        permulation(0, str, ans);
        sort(ans.begin(), ans.end());
        for (int i = 0; i < ans.size(); i++)
            cout<<ans[i]<<endl;
    }
 
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 632KB, 提交时间: 2020-10-31

// test.cpp : Defines the entry point for the console application.
//
//#define DEBUG
#ifdef DEBUG

#else

#include <bits/stdc++.h>
using namespace std;

#define vi vector<int>
#define si set<int>
#define pii pair<int,int>
#define piii pair<int, pii>
#define piiii pair<int, piii>
#define umii unordered_map<int,int>
#define lowbit(t) t&(-t)
#define x first
#define y second
#define sqr(x) ((x) * (x))
#define eps 1e-9
#define m_init(arr, val) memset(arr, val, sizeof arr)
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define SZ(x) int(x.size())
#define pi acos(-1)
#define mod 1000000007
#define MOD 998244353
#define INF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define bcase break;case
#define bdefault break;default
#define repi(i, a, b) for(int i = (a),i##len=(b); i<i##len; i++)
#define peri(i,a,b) for(int i=(a);i>=(b);i--)
#define perll(i,a,b) for(ll i=(a);i>=(b);i--)
#define repll(i, a, b) for(ll i = a; i<b; i++)
#define has_e(s,e) (s.find(e)!=s.end())
#define ceili(a, b) \
    ((a)/(b) + ((a)%(b) ? 1 : 0))
template <class U, class T> void Max(U &x, T y) { if (x<y)x = y; }
template <class U, class T> void Min(U &x, T y) { if (x>y)x = y; }
template <class T> void add(int &a, T b) { a = (a + b) % mod; }

int dx[] = {1,0,0,-1,1,1,-1,-1};
int dy[] = {0,1,-1,0,-1,1,-1,1};

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

ll qmul_mod(ll a, ll b, ll m) {
    ll ans = 0LL;
    while (b) {
        if (b & 1)ans = (ans + a) % m;
        a = (a + a) % m, b >>= 1;
    }
    return ans % m;
}

ll qpow_mod(ll x, ll y, ll m) {
    ll res=1;
    while(y)
    {
        if(y%2)(res*=x)%=m;
        (x*=x)%=m;
        y>>=1;
    }
    return res;
}

const int msize = 15;

struct Matrix {
    int m[msize][msize];
    Matrix() { memset(m, 0, sizeof m); }
    void print() {
        repi(i,0,msize) {
            repi(j,0,msize) {
                printf("%d%c", m[i][j], " \n"[j==msize-1]);
            }
        }
        puts("");
    }
    void Unit() { repi(i, 0, msize) m[i][i] = 1; }
};

inline Matrix operator * (Matrix a, Matrix b) {
    Matrix res;
    repi(i, 0, msize)
        repi(k, 0, msize)
            if (a.m[i][k])
                repi(j, 0, msize)
                    (res.m[i][j] += 1LL * a.m[i][k] * b.m[k][j] % mod) %= mod;
    return res;
}

Matrix qmat_pow(Matrix a, int n) {
    Matrix res;
    res.Unit();
    while (n)
    {
        if (n & 1) {
            res = res * a;
        }
        n >>= 1;
        a = a * a;
    }
    return res;
}

template <class T>
inline T lcm(T x,T y)
{
    T a, b, c;
    a = x;
    b = y;
    c = x%y;
    while(c!=0)
    {
        x = y;
        y = c;
        c = x%y;
    }
    return a*b/y;
}

template <class T>
inline T gcd(T __m, T __n)
{
    while (__n != 0)
    {
        T __t = __m % __n;
        __m = __n;
        __n = __t;
    }
    return __m;
}

#define N 30005

//ll f[N], inv[N];
//
//ll Combination(int a, int b) {
//    return b > a ? 0 : 1LL * f[a] * inv[b] % mod*inv[a - b] % mod;
//}
//
//void C_init() {
//    f[0] = 1;
//    repi(i, 1, N) {
//        f[i] = 1LL * i* f[i - 1] % mod;
//    }
//    inv[N - 1] = qpow_mod(f[N - 1], mod - 2, mod);
//    peri(i, N - 2, 0) {
//        inv[i] = 1LL * (i + 1)*inv[i + 1] % mod;
//    }
//}

int T = 1;
int n, m, k;

#ifdef SEG_TREE
struct node {
    int val;
}tree[N<<2];

void down(int p,int L,int R) {
    if (tree[p].val!=-1) {
        Max(tree[p<<1].val, tree[p].val);
        Max(tree[p<<1|1].val, tree[p].val);
        tree[p].val = -1;
    }
}

void up(int p,int L,int R) {
//    lsum[p] = lsum[p << 1];
//    rsum[p] = rsum[p << 1 | 1];
//    int mid = (L + R) >> 1;
//    if (lsum[p] == md - L + 1) {
//        lsum[p] += lsum[p << 1 | 1];
//    }
//    if (rsum[p] == R - md) {
//        rsum[p] += rsum[p << 1];
//    }
    if(L>R)
        return;
    tree[p].val = max(tree[p<<1].val, tree[p<<1|1].val);
}

void build(int p, int L, int R) {
    tree[p].val = -1;
    if (L == R) {
        return;
    }
    int mid = (L + R) >> 1;
    build(p << 1, L, mid);
    build(p << 1 | 1, mid + 1, R);
}

void upd(int p, int l, int r, int L, int R, int v) {
    if (l <= L && R <= r) {
        tree[p].val = v;
        return;
    }
//    down(p, L, R);
    int mid = (L + R) >> 1;
    if (l <= mid)
        upd(p << 1, l, r, L, mid, v);
    if (r > mid)
        upd(p << 1 | 1, l, r, mid + 1, R, v);
    up(p,L,R);
}

int query(int p, int l, int r, int L, int R) {
    if (l <= L && R <= r) {
        return tree[p].val;
    }
//    down(p,L,R);
    int mid = (L + R) >> 1;
    int ans = 0;
    if (l <= mid) {
        Max(ans, query(p << 1, l, r, L, mid));
    }
    if (r > mid) {
        Max(ans, query(p << 1 | 1, l, r, mid + 1, R));
    }
    return ans;
}
#endif

#ifdef BTREE
#define lowbit(t) t&(-t)
int a[50005];

void Update(int i, int v, int n) {
    while (i<=n) {
        a[i]+=v;
        i+=lowbit(i);
    }
}

int Sum(int i) {
    int res = 0;
    while (i) {
        res+=a[i];
        i-=lowbit(i);
    }
    return res;
}

#endif

#define PRIME
#ifdef PRIME
int prime[1000005], sift[1000005];

void sift_prime(int num) {
    sift[1]=1;
    repi(i,2,num+1) {
        if(!sift[i]) {
            prime[k++] = i;
        }
        for(int j=0; j<k && i*prime[j]<=num; j++) {
            sift[i*prime[j]] = 1;
            if (i%prime[j] == 0)
                break;
        }
    }
}
#endif

#ifdef UF
int fa[1000005];
int cnt[1000005];
int rnk[1000005];
//unordered_map<int, int> fa, cnt, rnk;
int components;
void Init(int n) {
    for (int i = 0; i < n; ++i) {
        fa[i] = i;
        cnt[i] = 1;
    }
    components = n;
}
//void Init(int n) {
//    if (!fa.count(n)) {
//        fa[n] = n;
//        cnt[n] = 1;
//        rnk[n] = 0;
//        ++components;
//    }
//}
int Find(int x) {
    int y = x;
    while (x != fa[x]) {
        x = fa[x];
    }
    return fa[y] = x;
}

void Union(int x, int y) {
    x = Find(x), y = Find(y);
    if (x != y) {
        if (rnk[x] > rnk[y]) {
            fa[y] = x;
            cnt[x] += cnt[y];
        }
        else {
            fa[x] = y;
            cnt[y] += cnt[x];
            if (rnk[x] == rnk[y])
                ++rnk[y];
        }
        --components;
    }
}
#endif

#ifdef BIG_OP
int o3[2005], o1[1005]={1}, o2[1005]={2};
int l_o3 = 0, l_o1 = 1, l_o2 = 1;
int one[1] = {1};
int big_add(int *res, const int *op1, const int *op2, int l_op1, int l_op2) {
    int carry = 0;
    int len = 0;
    memset(res, 0, sizeof(int)*(max(l_op2, l_op1)+1));
    repi(i,0,max(l_op2, l_op1)) {
        int tmp = carry;
        if (i<l_op1) {
            tmp += op1[i];
        }
        if (i<l_op2) {
            tmp += op2[i];
        }
        res[len++] = tmp%10;
        carry = tmp/10;
    }
    if (carry) {
        res[len++] = carry;
    }
    return len;
}

int big_mul(int *res, const int *op1, const int *op2, int l_op1, int l_op2) {
    int len = l_op1+l_op2-1;
    memset(res, 0, sizeof(int)*(len+1));
    repi(i,0,l_op1) {
        int carry = 0;
        repi(j,0,l_op2) {
            int tmp = res[i+j] + carry + op1[i] * op2[j];
            res[i+j] = tmp%10;
            carry = tmp/10;
        }
        if (carry) {
            res[i+l_op2] = carry;
            len = max(i + l_op2+1, len);
        }
    }
    return len;
}

//int *res = o3, *op1 = o1, *op2 = o2, l_res = l_o3, l_op1 = l_o1, l_op2 = l_o2;
//op1[0]
//repi(i,1,n) {
//l_res = big_mul(res, op1, op2, l_op1, l_op2);
//swap(op1, res);
//swap(l_op1, l_res);
////            peri(t,l_op1-1,0) {
////                printf("%d", op1[t]);
////            }
////            putchar('\n');
//l_res = big_add(res, op2, one, l_op2, 1);
//swap(op2, res);
//swap(l_op2, l_res);
//}

char n1[10005], n2[10005], n3[20005];
char * big_mul(char *res, const char *op1, const char *op2, int l_op1, int l_op2) {
    repi(i,0,l_op1+l_op2) {
        res[i] = '0';
    }
    peri(i,l_op1-1,0) {
        int carry = 0;
        peri(j,l_op2-1,0) {
            int tmp = res[i+j+1]-'0' + carry + (op1[i]-'0') * (op2[j]-'0');
            res[i+j+1] = tmp%10+'0';
            carry = tmp/10;
        }
        if (carry) {
            res[i] = carry+'0';
        }
    }
    repi(i,0,l_op1+l_op2-1) {
        if (res[i] != '0') {
            return res+i;
        }
    }
    return "0";
}
#endif

#ifdef EXP
char s[10005];

int dfs(int st, int ed) {
    int res = 0, sign = 1, num=0, p_num=0;
    char p_op = 0;
    int cnt = 0;
    repi(i,st,ed) {
        if (s[i] == '(') {
            if(!cnt++) {
                st=i;
            }
        } else if (s[i] == ')') {
            if (!--cnt) {
                num = dfs(st+1, i);
            }
        } else if (!cnt) {
            if (!isdigit(s[i])) {
                if (p_op) {
                    if (p_op == '*') {
                        num *= p_num;
                    } else {
                        num = p_num/num;
                    }
                    p_op = 0;
                }
                if (s[i] == '-' || s[i] == '+') {
                    res += sign * num;
                    num = 0;
                    if (s[i] == '-') {
                        sign = -1;
                    } else {
                        sign = 1;
                    }
                } else {
                    p_num = num;
                    num = 0;
                    p_op = s[i];
                }
            } else {
                num = num*10 + s[i] - '0';
            }
        }
    }
    if (p_op) {
        if (p_op == '*') {
            res += sign*p_num*num;
        } else {
            res += sign*p_num/num;
        }
    } else {
        res += sign*num;
    }
    return res;
}
#endif

#ifdef KMP
char s[15][100005];
char t[100005];
int nxt[100005];

void get_nxt(const char *s) {
    int j = -1;
    int i = 0;
    nxt[i] = j;
    int len = strlen(s);
    while (i < len) {
        if (j==-1 || s[i] == s[j]) {
//            if (s[++i] == s[++j]) {
//                nxt[i] = nxt[j];
//            } else {
//                nxt[i] = j;
//            }
            ++j,++i;
            nxt[i] = j;
        } else {
            j = nxt[j];
        }
    }
}

vector<pii> ranges;

void kmp(const char *s, const char *t) {
    int len1 = strlen(s), len2 = strlen(t);
    int i = 0, j = 0;
    while (i < len1) {
        if (j == -1 || s[i] == t[j]) {
            ++j;
            ++i;
        } else {
            j = nxt[j];
        }
        if (j == len2) {
            ranges.eb(i-j,i-1);
        }
    }
}
#endif

char s[1005];

int main() {
    srand(time(NULL)+clock());
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
//    freopen("out1.txt", "w+", stdout);
#endif
//    scanf("%d", &T);
//    while (T--)
    while (~scanf("%s", s))
//    repi(c,1,T+1)
    {
        n = strlen(s);
        sort(s,s+n);
        int i;
        while (1) {
            puts(s);
            for (i = n-1; i > 0; --i) {
                if (s[i] > s[i-1]) {
                    break;
                }
            }
            if (i==0)break;
            peri(j, n-1, i) {
                if (s[j] > s[i-1]) {
                    swap(s[j], s[i-1]);
                    break;
                }
            }
            reverse(s+i,s+n);
        }

    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
//    fclose(stdout);
#endif
    return 0;
}
#endif

上一题