Source Code
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <map>
#include <math.h>
#include <cmath>
#include <string.h>
#include <numeric>
using namespace std;
typedef vector<int> vi ;
typedef long long ll;
#define all(x) (x).begin() , (x).end()
#define allR(x) (x).rbegin() , (x).rend()
#define pb push_back
const int N = 1e6, M=1e6+1, NN=1e8 , MX = 1e6+1;
const ll MXL=1e18 , MOD = 1e9 + 7 ;
int a[N];
map<char,int> mp;
int main(){
    // freopen("input.txt", "r", stdin);
    int n, k;
    bool flag=0;
    cin >> n >> k;
    string s, ans, add, finalans;
    cin >> s;
    vector<pair<int, char>> v;
    for(int i=0 ; i<n ; i++ )
        mp[s[i]]++;
    for(char c='a' ; c<='z' ; c++)
        v.pb({mp[c],c});

    sort(allR(v));
    for(int i=0 ; i<v.size() ; i++ ){
        if( v[i].first >=k )
            ans+=v[i].second;
    }
    for(int i=0 ; i<v.size() ; i++ ){
        if( v[i].first && (v[i].first)%k >= (v[i].first)/k )
            add+=v[i].second;
    }
    // cout << ans <<"  "<<add<<" ";;
    if(!ans.size())
        puts("3");
    else{
        if(!add.size())
            puts("1");
        else
            puts("2");
        for(int i=0 ; i<k ; i++)
            finalans+=ans;
        finalans+=add;
        for(int i=0 ; i<n ; i++ )
            cout << finalans[i];
        puts("");
    }
    return 0 ;
}
Copy
Repeating Strings Rand()
GNU G++17
32 ms
1.7 MB
Fail to Judge
Test Case #1
1 ms
360 KB
Accepted
Input
12 3
aaccbbabcddd
Output
1
dcbadcbadcba
Judge Output
1
abcdabcdabcd
Checker Message
The type and the string are correct
Test Case #2
0 ms
388 KB
Accepted
Input
10 2
zigzagiaai
Output
2
iazgiazgia
Judge Output
2
aigzaigzai
Checker Message
The type and the string are correct
Test Case #3
0 ms
484 KB
Accepted
Input
7 4
fegtyta
Output
3
Judge Output
3
Checker Message
The string is of the type 3
Test Case #4
29 ms
1.7 MB
Accepted
Input
843011 148379
aksbudtkbphdbiverwnrdxntqdwbyhemsmkkjuqmejtfzdvwseeivlfbeeygsxsjkjtzhnnyklrahltppzwvicotmjyjckdaxqabahqxdiksiwopkdwecgymjxdhjqmkvurvujemovqycdhdlfvxhegbbhlzhkscykonkzusoymktoafzsvdmqusqlsoaqifsayosgytkzsztucgrycqasivtshsejpknmdqzsnhizk...
Output
3
Judge Output
3
Checker Message
The string is of the type 3
Test Case #5
32 ms
1.7 MB
Accepted
Input
959139 926507
uywqcfkbfwlniomreqcmunpcdeymhmxqsvkgyrbiolnfgnzlczyobkoncjhixnncaoaakrupfstigreivmpttrhlmodpxmieeikgzpmdmrwbbmyjhschxgdvmvakxeepsoghakcmpnrtdutugbdalnryvwpcymyuicsglymksdkwfysrmyylwkeatxchcxwoohascwtvehtfbxgpbmsxypicozibdwvbhafwtlvjlkp...
Output
3
Judge Output
3
Checker Message
The string is of the type 3
Test Case #6
1 ms
756 KB
Accepted
Input
9859 7515
wpyigtqspdxdgddtsioeudsmnojjkcamohulielbmhjisfuypwsujpmvqbujcfdlmiqmlrntfjvfhhjbnmsqgdknzjaycxpfgfzwpppktpqqcdwcxtxunfqnfyubmqekntjgqdyslewbmvkgqhidgvdbxtqgqenvxehelpbirdsyqyoycpldxngydiurzmigkovwqfcvssasjtpgbwjmieloaqdcfqutbbrpomhdrngukav...
Output
3
Judge Output
3
Checker Message
The string is of the type 3
Test Case #7
5 ms
988 KB
Accepted
Input
125987 49792
anaoteycowwesxfybczrvxktnpcgeqlxgzzinjgritwjvrqjvvfhzxvyjdireflitjcuxxsvcnwuucjlmfxpprumqqhvokjnhaaboesidsdxernnklxszmihxzmkbwrxyzjpwuulinvycoifkgdebbgbefxjuezabszhyqlxppljkeykmwtqrjbqyezfjjmyrmscgorqngkgtpwxqojsyuabcbvsoxatwgbvhnoyvitw...
Output
3
Judge Output
3
Checker Message
The string is of the type 3
Test Case #8
7 ms
1.0 MB
Accepted
Input
209411 76302
ktzqqcfbuopjvdkofcsdgrunmbiishnaduqoisioisozduojprzbpjusrdvlcewzpatgbikbualpuootbnjrzdvakgtavwzbbyrdzcgtlfffrkpubwwprpqvclldsvhvxvzqfjqugbbvzardvyzmltmegprguzetqmorjcnovqgpbfrhkzkupszdgbzajfnjpfklmbbaygluospwufgieqniltynjhanakhkphjrxagj...
Output
3
Judge Output
3
Checker Message
The string is of the type 3
Test Case #9
11 ms
1.4 MB
Accepted
Input
325539 230213
oeuwsgvfnskqwdijjludwtxdhkwgphpgglcrwlypjwxfjuwhuuilhxkuvebnsxfcicrtlrzvnfxeiiprfrlzmtwauclbjqdbrkvbbvtsgtzofthlqedgbfpmejqtxpxfveqlobozuzwlkkapllcwllydkygzozdidcdzpcwqpbpbsimdevwahpinibzlfjkfgkaajdnfcsjlrvfngjxxvlutjuuuyvepgefkyzrnbfm...
Output
3
Judge Output
3
Checker Message
The string is of the type 3
Test Case #10
0 ms
0 KB
Running
Input
843011 247
fqilwnuqirjdfzdunivjbftpuxtlwzgepmgdogqzvmlkctervukcjjhooxjkojevjjbgqchbnnctkrwpupqnikqzrqtsrplrudpxhcicqtasqhpfbswtelmlqvvoezyuproduwtodpkapejxsmrgzxcfbxvzzvkyhqfmxexwbbxyetklemaobplnbijvcdseknxtvzdabtbezllopavcsgtwossjgapfpvzmhidtyjmihu...
Output

Judge Output
1
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
Checker Message