Source Code
#include <iostream>
using namespace std;
int freq[26], mod[26];
int main() {
	int n, k;
	string s;
	cin >> n >> k >> s;
	for(int i = 0; i < n; ++i){
		freq[s[i] - 'a']++;
	}
	for(int i = 0; i < 26; ++i){
		if(freq[i] < k && freq[i]){
			cout << 3 << endl;
			return 0;
		}
	}
	bool u = 0;
	for(int i = 0; i < 26; ++i){
		if(freq[i] % k){
			u = 1;
		}
		mod[i] = freq[i] % k;
	}
	if(!u){
		for(int i = 0; i < n; ){
			for(int j = 0; j < 26; ++j){
				for(int l = 0; l < freq[j] / k; ++l){
					cout << (char)('a' + j);
					++i;
				}
			}
		}
	}
	else{
		cout << 2 << endl;
		int l = 0, c = 0;
		while(c < k){
			for(int i = 0; i < 26; ++i){
				for(int j = 0; j < mod[i]; ++j){
					cout << (char)('a' + i);
				}
			}
			for(int i = 0; i < 26; ++i){
				for(int j = 0; j < freq[i] / k - mod[i] && freq[i]; ++j){
					cout << (char)('a' + i);
				}
			}
			++c;
		}
		for(int i = 0; i < 26; ++i){
			for(int j = 0; j < mod[i]; ++j){
				cout << (char)('a' + i);
			}
		}
	}
	return 0;
}
Copy
Repeating Strings azoz
GNU G++17
0 ms
360 KB
Wrong Answer