CSU - 1828 康托展开

The isolated people of MacGuffin Island have a unique culture, and one of the most interesting things about them is their language. Their alphabet consists of the first 9 letters of the Roman alphabet (a, b, c, d, e, f, g, h, i). All of their words are exactly 9 letters long and use each of these 9 letters exactly once. They have a word for every possible permutation of these letters. In the library of their most sacred temple is a dictionary, and each word in their language has its own page. By coincidence they order their words exactly as they would be in ordered in English, so the word ‘abcdefghi’ is on the first page, and the word ‘ihgfedcba’ is on the last. The question is, given a list of random words from the MacGuffin language, can you say on which page of the MacGuffin dictionary each appears?

Input

The first line of the input file is a positive integer. This integer tells you how many words will follow. The upper limit for this number is 6000. Every subsequent line contains a single word from the MacGuffin language, so if the first number is 1000 there will be 1000 lines after it, each containing a single word.

Output

Each line of output will contain an integer. This integer should be the page number for the corresponding word.

Sample Input

4
abcdefgih
abcdefghi
abcdefgih
ihgfedcba

Sample Output

2
1
2
362880


#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

char c[15];
int jie[15];

void jiec() {
jie[1]=1;
for(int i=2;i<=9;i++)
jie[i]=jie[i-1]*i;
}

int cantor(char* c) {
int cnt=0,ret=0;
for(int i=0;i<9;i++)
{
cnt=0;
for(int j=i+1;j<9;j++)
if(c[i]>c[j])
cnt++;
ret+=cnt*jie[8-i];
}
return ret;
}

int main() {
int T;
scanf("%d",&T);
jiec();
while(T--)
{
scanf("%s",c);
printf("%d\n",cantor(c)+1);
}

return 0;
}

转载于:https://www.cnblogs.com/xibeiw/p/7394467.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值