C. Phoenix and Distribution [CF1348 C] (Dictionary order, thinking)

C. Phoenix and Distribution

CF1348 C
question meaning: given string s, each character in it is required to be divided into kkk non-empty strings. It is required to output the minimum value of the largest lexicographic string in all sub-methods.
Idea:
First of all, ensure that it is not empty: if the smallest number of characters cannot fill k, directly output the k-th character after the order. (All remaining characters can be placed after the smallest character)
Then pressEvenly splitThe thought goes:

  • If all the remaining values ​​are the same, the longest is divided equally.
  • If they are not the same, output the remaining characters in lexicographic order from smallest to largest.
int vis[30],v[30];
int n,m,a,b,co=0,nw,tn,fla;
char tt;
int ju(){
    
    
    fla=1;
    if(co==1||(vis[v[1]]==0&&co==2)){
    
    
        tt='a'+v[1];cout<<tt;
        for(int i=1;i<=co;i++){
    
    
            for(int j=1;j<=vis[v[i]]/m+(vis[v[i]]%m!=0);j++){
    
    tt='a'+v[i];cout<<tt;}
        }
        cout<<endl;
    }
    else fla=0;
    return fla;
}
int main(){
    
    
    int t;
    cin>>t;
    string s;
    while(t--){
    
    
        mem(vis,0);
        co=0;
        cin>>n>>m>>s;
        for(int i=0;i<n;i++){
    
    
            if(vis[s[i]-'a']==0){
    
    
                v[++co]=s[i]-'a';
            }
            vis[s[i]-'a']++;
        }
        sort(v+1,v+1+co);
        if(vis[v[1]]>=m){
    
    
            vis[v[1]]-=m;
            if(ju()==1)continue;
            tt='a'+v[1];cout<<tt;
            nw=1;
            if(vis[v[nw]]==0)nw++;
            for(int i=1;i<=n-m;i++){
    
    
                tt='a'+v[nw];cout<<tt;
                vis[v[nw]]--;
                if(vis[v[nw]]==0){
    
    nw++;}
            }
            cout<<endl;
            continue;
        }
        else{
    
    
            nw=0;fla=1;
            for(int i=1;;i++){
    
    
                if(nw+vis[v[fla]]>=m){
    
    
                    tt='a'+v[fla];cout<<tt<<endl;
                    break;
                }
                nw+=vis[v[fla++]];
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_44986601/article/details/105932179