问题:求给出一个数字 k k k,求一个最短的字符串,该字符串的所有子序列中 c o d e f o r c e s codeforces codeforces出现至少k次(答案不唯一)。
举个例子:k=4,字符串可以是 c c o o d e f o r c e s ccoodeforces ccoodeforces
思路: c o d e f o r c e s codeforces codeforces中每位置上的字符出现的次数相乘就是子序列 c o d e f o r c e s codeforces codeforces出现的次数。
#include<bits/stdc++.h>
using namespace std;
char s[] = "codeforces";
int main() {
long long k, sum = 1;
cin >> k;
if(k == 1) {
cout << s;
return 0;
}
//枚举前j个字母要重复i次,后10-j个字母重复i-1次。
//每次只会加一个字母,所以能够在字符串最短的时候满足条件。
for(int i=2; i<100; i++) {
for(int j=0; j<10; j++) {
sum = sum/(i-1) * i;
if(sum >= k) {
for(int h=0; h<=j; h++) {
for(int u=0; u<i; u++) {
cout << s[h];
}
}
for(int h=j+1; h<10; h++) {
for(int u=0; u<i-1; u++) {
cout << s[h];
}
}
return 0;
}
}
}
return 0;
}