B. Codeforces Subsequences (thinking, violence)

B. Codeforces Subsequences

Question: Find a number kkk , find a shortest string,codeforces codeforcesin all subsequences of the stringc o d e f o r c e s appears at least k times (the answer is not unique).

For example: k=4, the string can be ccoodeforces ccoodeforcesccoodeforces

Idea: codeforces codeforcesc o d e f o r c e s multiplied by the number of occurrences of the character in each position is the subsequencecodeforces codeforcesThe number of occurrences of c o d e f o r c e s .

#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;
}

Guess you like

Origin blog.csdn.net/weixin_45363113/article/details/106878766