字典顺序排在第k位(只有a,b)
Among the strings of length A+B containing A occurrences of a and B occurrences of b, find the string that comes K-th in the lexicographical order.
Constraints
1≤A,B≤30
1≤K≤S, where S is the number of strings of length A+B containing A occurrences of a and B occurrences of b.
All values in input are integers.
Constraints
1≤A,B≤30
1≤K≤S, where S is the number of strings of length A+B containing A occurrences of a and B occurrences of b.
All values in input are integers.
输入
Input is given from Standard Input in the following format:
A B K
A B K
输出
Print the answer.
样例输入 Copy
【样例1】
2 2 4
【样例2】
30 30 118264581564861424
样例输出 Copy
【样例1】
baab
【样例2】
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
提示
样例1解释
Here are the strings containing two as and two bs in the lexicographical order: aabb, abab, abba, baab, baba, and bbaa. The fourth string, baab, should be printed.
样例2解释
K may not fit into a 32-bit integer type.
Here are the strings containing two as and two bs in the lexicographical order: aabb, abab, abba, baab, baba, and bbaa. The fourth string, baab, should be printed.
样例2解释
K may not fit into a 32-bit integer type.
题意:这个题就是构造一个长度为A+B的字符串,其中包含A个a和B个b,并且字典序为第k位。
这个题就是一个组合数的问题:
构造一个全是a的字符串,然后从大到小枚举每一个字符,放上b,
如果说i是放第i个字符,z是第z位。
c=C[z][b-i+1];
d=C[z+1][b-i+1];
这个就假如b放在第i位后面还有k个字符,其中有z个b字符,然后他的字典数位c[z][k]
就是这样判断的
#include<iostream> #include<algorithm> #include<cstring> typedef long long ll; using namespace std; const int maxn=1e3+100; int b[maxn]; ll C[maxn][maxn]; void get_C() { C[0][0] = 1; for(int i=1;i<=100;i++) { C[i][0] = 1; for(int j=1;j<=i;j++) C[i][j] = C[i-1][j]+C[i-1][j-1]; } } int main(){ get_C(); ll a,b,k; cin>>a>>b>>k; k--; ll n=a+b; vector<char> v(n,'a'); int p=a; ll z=n; for(int i=1;i<=b;i++){ ll c,d; for(;z>=1;z--){ c=C[z][b-i+1]; d=C[z+1][b-i+1]; if(c <= k && d > k){ break; } } k-=c; v[z]='b'; } reverse(v.begin(),v.end()); for(int i = 0; i < n; i++) cout << v[i]; cout << endl; }