Codeforces Round 921 (Div. 2)

A. We Got Everything Covered!

题意:

给你正整数 n n n k k k , 要你构造出一个字符串 s s s ,使得所有可能的长度为 n n n的由前 k k k个字母组成的字符串都是字符串 s s s的子序列 。

题解:

构造一个字符串 a b c . . . a + k abc...a+k abc...a+k,此字符串输出n遍就是结果。

代码:

#include <iostream>
using namespace std;
int main()
{
	int t;
	cin>>t;
	
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<k;j++)
			{
				cout<<(char)('a'+j);
			}
		}
		cout<<'\n';
	}
	return 0;
}

B. A Balanced Problemset?

题意:

x x x分成 n n n份,使得这 n n n个数的 g c d gcd gcd最大,输出最大的 g c d gcd gcd

题解:

x = x= x=GCD* x 1 x_{1} x1 +GCD* x 2 x_{2} x2 +GCD* x 3 x_{3} x3 +…+GCD* x n x_{n} xn = = =GCD( x 1 x_{1} x1+ x 2 x_{2} x2+ x 3 x_{3} x3 +… + x 1 x_{1} x1 = = = G C D ∗ s u m GCD*sum GCDsum
所以 G C D = x / s u m GCD=x/sum GCD=x/sum,因为每个元素都必须是正整数,所以 s u m > = n sum>=n sum>=n,可以枚举x的因数,通过 G C D = x / s u m GCD=x/sum GCD=x/sum,算出gcd,取出最大的就是答案。

代码:

#include<iostream>
#include<algorithm>

using namespace std;

void solve(){
	int x,n;
	int ans=0;
	
	cin>>x>>n;
	
	for(int i=1;i*i<=x;i++){
		if(x%i==0){
			if(i>=n) ans=max(ans,x/i);
			if(x/i>=n) ans=max(ans,i);
		}
	}
	cout<<ans<<endl;
}
int main(){
	int t;
	
	cin>>t;
	
	while(t--) solve();
	
	return 0;
}

C. Did We Get Everything Covered?

题意:

给出一个字符串 s s s n n n k k k,若任意长度为 n n n且由前 k k k个小写字母组成的字符串都是 s s s子字符串就输出 y e s yes yes,否则输出 n o no no,并输出 s s s不包含的子字符串。

题解:

当s满足可分割为n份,且这n份都包含前k个小写字母时才会输出yes,否则需要构造一个s不存在的子字符串t,在分割成的n份中,若包含前k个小写字母,就取这一小份的最后一个元素,否则取该小份不包含的小写字母,这样就可以组成t。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#define int long long

using namespace std;

void solve(){
	int n,k,m;
	string s;
	vector<char> temp;
	set<char> se;
	
	int cnt=0;
	
	cin>>n>>k>>m;
	
	cin>>s;
	for(int i=0;i<m;i++)
	{
		se.insert(s[i]);
		if(se.size()==k){
			cnt++;
			temp.push_back(s[i]);
			se.clear();
		}
	}
	if(cnt>=n){
		cout<<"YES"<<endl;
		return;
	}
	else{
		cout<<"NO"<<endl;
		int i;
		
		for(i=0;i<temp.size();i++) cout<<temp[i];
		for(int j=0;j<k;j++){
			if(se.find(char(j+'a'))==se.end())
				for(;i<n;i++)  cout<<char(j+'a');
		}
		cout<<endl;
	}
}
signed main(){
	int t;
	
	cin>>t;
	
	while(t--) solve();
	
	return 0;
}


  • 31
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值