nyoj 139 我排第几个

 

我排第几个

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述

现在有"abcdefghijkl”12个字符,将其所有的排列中按字典序排列,给出任意一种排列,说出这个排列在所有的排列中是第几小的?

 
输入
第一行有一个整数n(0<n<=10000);
随后有n行,每行是一个排列;
输出
输出一个整数m,占一行,m表示排列是第几位;
样例输入
3
abcdefghijkl
hgebkflacdji
gfkedhjblcia
样例输出
1
302715242
260726926
来源
[苗栋栋]原创
上传者
苗栋栋
康托定理 不知道可以直接百度
View Code
 1  
 2  
 3 #include<cstdio>
 4 const int Fact[13]={1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600};
 5 int getTh(char code[],int n)
 6 {
 7     int sum=0;
 8     for(int i=0;i!=n;i++)
 9     { 
10         int rev=0;
11         for(int j=i+1;j<n;j++)
12         rev+=(code[i]>code[j]);
13         sum+=rev*Fact[n-1-i]; //Fact[i]表示i的阶乘。
14     }
15     return sum;
16 }
17 int main()
18 {
19     int n;
20     char str[15];
21     scanf("%d",&n);
22     while(n--)
23     {
24         scanf("%s",str);
25         printf("%d\n",getTh(str,12)+1);
26     }
27 }                
View Code
 1  
 2 /*********************************
 3 /   Problem:
 4 /   Algorithm:
 5 /   Language:   C++
 6 /   Compiler:   MinGW
 7 /   Date:       12/08/08
 8 /
 9 /   Copyright (C) wujianwei
10 /   All rights reserved.
11 ********************************/
12 
13 #include <iostream>
14 #include <cstdio>
15 #include <cstring>
16 #include <cmath>
17 #include <vector>
18 #include <cstring>
19 #include <queue>
20 #include <stack>
21 #include <algorithm>
22 #include <set>
23 
24 using namespace std;
25 
26 #define INF 0x7fffffff
27 #define EPS 1e-12
28 #define MOD 1000000007
29 #define PI 3.141592653579798
30 #define N 100005
31 const int MAX=1<<28;
32 typedef long long LL;
33 //typedef __int64 INT
34 
35 char s2[12];
36 
37 LL jie(int a)
38 {
39     LL ans=1;
40     for(int i=2;i<=a;i++)
41     {
42         ans*=i;
43     }
44     return ans;
45 }
46 
47 int main()
48 {
49     int T;
50     LL a,b,c;
51     scanf("%d",&T);
52     while(T--)
53     {
54         scanf("%s",s2);
55         LL sum=1;
56         int a;
57         bool s[12]={0};
58         int t=0;
59         for(int i=0;i<12;i++)
60         {
61             t=0;
62             a=s2[i]-'a';
63             s[a]=1;
64             for(int j=0;j<12;j++) if(!s[j]&&a>j) t++;
65             sum+=t*jie(11-i);
66         }
67         cout<<sum<<endl;
68     }
69     return 0;
70 }
71         

还有个题目是逆康托定理

因为忘记了是那个题目 下次看到再加进来

嘿嘿

posted on 2012-08-13 20:58  不悔梦归处  阅读(245)  评论(0编辑  收藏  举报

导航