Source Code
#include <bits/stdc++.h>

#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

typedef long long ll;
typedef long double ld;
const int N = 1e6 + 15, lg = 20;
int mod = 1e9 + 7;
int inf = 1e9;
map<char, int> co;
int k, n;

void check_one() {
    bool good = 1;
    for (auto it:co)
        good &= (it.second % k == 0);
    if (!good)return;
    string res = "";
    for (auto it:co)
        res += string(it.second / k, it.first);
    cout << "1\n";
    for (int i = 0; i < k; i++)
        cout << res;
    cout << endl;
    exit(0);

}

void check_two() {
    string res = "", p = "";
    for (auto it:co) {
        if (it.second % k > it.second / k)return;
        res += string(it.second / k - (it.second % k), it.first);
        res = string(it.second % k, it.first) + res;
        p = string(it.second % k, it.first)+p ;
    }

    cout << "2\n";
    for (int i = 0; i < k; i++)
        cout << res;
    cout << p;
    cout << endl;
    exit(0);

}

int main() {
    FIO
    string s;
    cin >> n >> k >> s;
    for (auto it:s)
        co[it]++;
    check_one();
    check_two();
    cout << 3;


    return 0;
}
Copy
Repeating Strings ELbadawe
GNU G++17
31 ms
3.1 MB
Accepted