Source Code
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>
 
using namespace std;
int f[30];
int main(){
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    int k,n;
    string s;
    cin>>n>>k>>s;
    for(int i=0;i<n;i++){
        f[(s[i]-'a')]++;
    }
    bool b=0,b2=0;
    for(int i=0;i<30;i++){
        if(f[i]!=0){
            if(f[i]<k){
                b=1;
            }
            if(f[i]>k){
                b2=1;
            }
        }
    }
    if(b){
        return cout<<3,0;
    }
    if(b2){
        cout<<2<<endl;
    }
    else{
        cout<<1<<endl;
    }
    vector<pair<int,int>>v;
    for(int i=0;i<30;i++){
        if(f[i]!=0){
            v.push_back(make_pair(f[i],i));
        }
    }
    sort(v.begin(),v.end());
    k++;
    while(k--){
        for(int i=v.size()-1;i>=0;i--){
            char h=('a'+v[i].second);
            if(v[i].first!=0){
                cout<<h;
                v[i].first--;
            }
        }
    }
    
    return 0;
}
Copy
Repeating Strings oora
GNU G++17
4 ms
1.7 MB
Wrong Answer