B题
很容易可以判断出最终的结果肯定是某个长度为k的字串不停地循环,问题关键在于如何确定这个子串,即怎么在原序列中找一个长度为k的子序列。任意一个长度为k的子序列肯定不行,因为如果原序列中存在不在这个子序列中的其他数,那么就无法表示,所以我们找原序列中不同的数的个数。如果原序列中不同的数个数大于k肯定不能用循环长度为k的序列表示出来;如果小于等于k那么我们就把这些不同的数当作基础串来循环输出,不足k个补其他数。
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<cstring>
#include<bitset>
#include<queue>
#define lson (rt << 1)
#define rson (rt << 1 | 1)
const int maxn = 100+50;
const int maxm = 4e5 + 10;
const int inf_max = 0x3f3f3f;
using namespace std;
typedef long long ll;
int n,a[maxn],k,ans[maxn * maxn];
set<int>st;
int main()
{
int t;
scanf("%d",&t);
while(t--) {
cin>>n>>k;
st.clear();
for(int i = 1;i <= n; ++i) {
cin>>a[i];st.insert(a[i]);
}
int tt = st.size();
if(tt > k) {
cout<<-1<<endl;continue;
}
cout<<n*k<<endl;
for(int i = 1;i <= n; ++i) {
for(auto b : st) printf("%d ",b);
for(int j = 0;j < k - (int)st.size(); ++j) printf("1 ");
}
printf("\n");
}
return 0;
}
C题
首先将字符串排序,然后模拟一下知道前k个如果不一样的话,答案就是最大的那个,只有在一样的情况下才考虑后事。在前k个字符一样的条件下,后面的字符如果有两种及以上的种类,那么最大的种类越晚添加,则整体就最小,所以直接把所有的字符加在后面;如果后面的字符一样,那么就给k组平均分。
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<cstring>
#include<bitset>
#include<queue>
#define lson (rt << 1)
#define rson (rt << 1 | 1)
const int maxn = 100+50;
const int maxm = 4e5 + 10;
const int inf_max = 0x3f3f3f;
using namespace std;
typedef long long ll;
string str;
int n,k;
int main()
{
int t;
scanf("%d",&t);
while(t--) {
cin>>n>>k>>str;
sort(str.begin(),str.end());
int len = str.size();
if(str[0] != str[k - 1]) {
cout<<str[k-1]<<endl;
continue;
}
cout<<str[0];
if(str[k] != str[n - 1]) {
for(int i = k;i < n; ++i) cout<<str[i];
}else {
int up = (len - k) / k;
if((len - k) % k) up++;
for(int i = 1;i <= up; ++i)cout<<str[n - 1];
}
cout<<endl;
}
return 0;
}