nyoj 139 我排第几个

来源:互联网 发布:mysql linux安装后在哪 编辑:程序博客网 时间:2024/05/02 00:11

题目链接:我排第几个

题目就是给你一个序列,问你这个序列在字典序里面排第几。典型的康拓展开题目,模板见博客:

#include <bits/stdc++.h>using namespace std;typedef long long ll;ll f[105],t;char ss[15];ll cantor(char str[]){    int len = strlen(str);    ll ans = 0;    for(int i = 0;i < len;i++){        int tmp = 0;        for(int j = i+1;j < len;j++)            if(str[j] < str[i]) tmp++;        ans += tmp*f[len-i-1];    }    return ans;}int main(){    f[0] = 1;    for(int i = 1;i <= 26;i++)        f[i] = f[i-1]*i;    cin>>t;    while(t--){        cin>>ss;        cout<<cantor(ss)+1<<endl;      }    return 0;}

0 0