局限性,
1.由于要处理阶乘,当阶乘太大的时候阶乘数组会爆精度。
2.只可以处理一个排列的字典序(就是全部字符都要不一样)
AC代码
#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-5;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
char str[20];//存字典序的字符
ull f[20],arr[20],n;
ull cantor()//康托展开
{
ull ans = 0;
for(int i = 0;i < n;i++)
{
ull x = 0;
for(int j = i+1;j < n;j++)
if(str[i]-str[j] > 0)
x++;
ans += (x*f[n-i-1]);
}
return ans;
}
void incantor(ull k)//逆康托展开
{
vector<ull> v;
for(ull i = 1;i <= n;i++) v.push_back(i);
for(ull i = 1;i < n;i++)//v数组的第一个元素位置是0,所以下面也要相应改变
{
ull x = k/f[n-i];
arr[i] = v[x];
v.erase(v.begin()+x);
k %= f[n-i];
}
arr[n] = v[0];
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin >> n;f[1] = f[0] = 1;
for(int i = 0;i < n;i++) cin >> str[i];
for(int i = 2;i <= n;i++) f[i] = f[i-1]*i;//预处理阶乘
incantor(cantor()-1);
for(int i = 1;i <= n;i++) cout << arr[i] << " ";//存这个字典序的上一个排列,还可以处理字母的字典序
}