Source Code
// •︿• \\
/\\    //\
 /\\  //\
  /\\//\
  
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <math.h>
#include <string>
#include <cstring>
#include <cassert>
using namespace std;
 
const float PI = acos(-1);
const int N=1e5 + 10;
// const int M=1000000007;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define oo 1000000001


void solve(){
	int n,k;
	cin>>n>>k;
	string s;
	cin>>s;
	vi freq(26,0);
	for (int i = 0; i < n; ++i)
	{
		freq[s[i]-'a']++;
	}
	string a[2]={"",""};
	string p="";
	bool last=false;
	for (int i = 0; i < 26; ++i)
	{
		// printf("for %c\n",i+'a' );
		if(freq[i]%k){
			int in_a,in_p;
			in_a = freq[i]/k;
			in_p = freq[i]%k;
			// printf("for %c in_a %d in_p %d\n",i+'a',in_a,in_p );
			if(in_p>in_a){
				printf("3\n");
				return;
			}
			
			for (int j = 0; j < min(in_a,in_p); ++j)
			{
				a[0]+=(i+'a');
				p+=(i+'a');
			}
			for (int j = 0; j < in_a - min(in_a,in_p); ++j)
			{
				a[1]+=(i+'a');
			}

		}else{
			for (int j = 0; j < freq[i]/k; ++j)
			{
					a[1]+=(i+'a');
			}
		}
	}
	// cout<<a[0]<<" "<<a[1]<<" "<<p<<"\n";
	int l = 1;
	if(p.size()==0){
		printf("1\n");
		for (int i = 0; i < k; ++i)
		{
			cout<<a[0]<<a[1];
		}
		printf("\n");
		return;
	}
	printf("2\n");
	for (int i = 0; i < k; ++i)
	{
		cout<<a[0]<<a[1];
	}
	printf("%s\n",p.c_str() );
	return;
}

 
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--)
		solve();

	return 0;
}
Copy
Repeating Strings Saq
GNU G++17
19 ms
3.1 MB
Accepted