C. Phoenix and Distribution

探讨了如何将一个字符串分割成k个子串,使其中字典序最大的字符串达到最小化。通过排序和贪心策略,分析了不同场景下的最优分配方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Phoenix and Distribution

题意
给了一个长度为n的字符串,让你将字符串分成k个字符串,这k个字符串不能存在空串,现在想要知道,这k个字符串中 字典序最大的最小的字符串是什么。

思路
第一步肯定是对字符串排序。
考虑一下,如果前k小的字符不全一样的话,那么肯定是已经有了大小结果。就是第k个字符单独成串这样满足字典序最大的字符串最小。为什么?因为第一个字符久已经可以判断大小关系了。不管后面我怎么分配剩余的字符串,第一个字符最大的那个肯定是字典序最大的,但我想让他最小,那很简单,我只需要把剩余的分配给其他k-1个字符串即可,就一个贪心的思想。
那么如果第一个字符串相同呢?那么说明暂时无法比出来大小。
那么我们需要判断剩余的n-k个字符是不是都一样。
如果都一样,我们肯定是把这n-k个字符平均分给k个字符串。
如果存在不一样?直接把剩余的n-k个字符放在一个字符串后面。
为什么?考虑一下,如果剩余的n-k个不完全一样,那么必定有一个最小字符,一个最大字符
如果把这n-k个均分的话,字典序最大的会更大。
比如s=aabc ,k=2
先给两个字符串分配了一个字符a,如果均分的话字符串就是 ab、ac 最大字典序是ac
如果把剩余接在一个后面的话就是a、abc,最大字典序是abc abc<ac
分析完后 代码模拟一下即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){

    int t;cin>>t;
    while(t--){
       int len,k;cin>>len>>k;
       string s;cin>>s;
       sort(s.begin(),s.end());
       if(s[0]!=s[k-1]) cout<<s[k-1]<<endl;
       else {
            cout<<s[k-1];
            if(s[k]==s[len-1]){
                int q=(len-k)/k;
                if((len-k)%k) q++;
                while(q--) cout<<s[k];
            }
            else for(int i=k;i<len;i++) cout<<s[i];
            cout<<endl;
       }

    }
    return 0;
}
java.sql.SQLException: ERROR 726 (43M10): Inconsistent namespace mapping properties. Ensure that config phoenix.schema.isNamespaceMappingEnabled is consistent on client and server. at org.apache.phoenix.exception.SQLExceptionCode$Factory$1.newException(SQLExceptionCode.java:607) at org.apache.phoenix.exception.SQLExceptionInfo.buildException(SQLExceptionInfo.java:217) at org.apache.phoenix.query.ConnectionQueryServicesImpl.checkClientServerCompatibility(ConnectionQueryServicesImpl.java:1731) at org.apache.phoenix.query.ConnectionQueryServicesImpl.ensureTableCreated(ConnectionQueryServicesImpl.java:1460) at org.apache.phoenix.query.ConnectionQueryServicesImpl.createTable(ConnectionQueryServicesImpl.java:1939) at org.apache.phoenix.schema.MetaDataClient.createTableInternal(MetaDataClient.java:3090) at org.apache.phoenix.schema.MetaDataClient.createTable(MetaDataClient.java:1117) at org.apache.phoenix.compile.CreateTableCompiler$CreateTableMutationPlan.execute(CreateTableCompiler.java:420) at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:443) at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:425) at org.apache.phoenix.call.CallRunner.run(CallRunner.java:53) at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:424) at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:412) at org.apache.phoenix.jdbc.PhoenixStatement.executeUpdate(PhoenixStatement.java:1990) at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:3312) at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:3266) at org.apache.phoenix.util.PhoenixContextExecutor.call(PhoenixContextExecutor.java:76) at org.apache.phoenix.query.ConnectionQueryServicesImpl.init(ConnectionQueryServicesImpl.java:3266) at org.apache.phoenix.jdbc.PhoenixDriver.getConnectionQueryServices(PhoenixDriver.java:255) at org.apache.phoenix.jdbc.PhoenixEmbeddedDriver.createConnection(PhoenixEmbeddedDriver.java:144) at org.apache.phoenix.jdbc.PhoenixDriver.connect(PhoenixDriver.java:221) at sqlline.DatabaseConnection.connect(DatabaseConnection.java:135) at sqlline.DatabaseConnection.getConnection(DatabaseConnection.java:192) at sqlline.Commands.connect(Commands.java:1364) at sqlline.Commands.connect(Commands.java:1244) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sqlline.ReflectiveCommandHandler.execute(ReflectiveCommandHandler.java:38) at sqlline.SqlLine.dispatch(SqlLine.java:730) at sqlline.SqlLine.initArgs(SqlLine.java:410) at sqlline.SqlLine.begin(SqlLine.java:515) at sqlline.SqlLine.start(SqlLine.java:267) at sqlline.SqlLine.main(SqlLine.java:206) sqlline version 1.9.0
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会c语言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值