Educational Codeforces Round 107 (Rated for Div. 2) D. Min Cost String (贪心,构造)
-
题意:给你两个正整数\(n\)和\(k\),要你构造一个只包含前\(k\)个字母的字符串(\([a,a+k-1]\)),使得对于任意两个不相交的连续长度为\(2\)的区间,相等的子串数最少.
-
题解:我们一共有\(aa,ab,...,ba,bb,...,ca,cb...,a+(k-1),...\),这样的不同长度为\(2\)的子串,所以我们要\(a,ab,ac,...,b,bc,...,c,cd\)这样构造,少了的话就重复即可.
-
代码:
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define pb push_back #define me memset #define rep(a,b,c) for(int a=b;a<=c;++a) #define per(a,b,c) for(int a=b;a>=c;--a) const int N = 1e6 + 10; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; using namespace std; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll lcm(ll a,ll b) {return a/gcd(a,b)*b;} int main(){ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,k; cin>>n>>k; if(k==1){ rep(i,1,n){ cout<<'a'; } } else if(n<=k){ for(char c='a';c<'a'+n;++c){ cout<<c; } } else{ string s; for(char c='a';c<'a'+k;++c){ s+=c; for(char cc=c+1;cc<'a'+k;++cc){ s+=c; s+=cc; } } int cur=0; rep(i,1,n){ cout<<s[cur]; cur++; if(cur==(int)s.size()) cur=0; } } return 0; }
𝓐𝓬𝓱𝓲𝓮𝓿𝓮𝓶𝓮𝓷𝓽 𝓹𝓻𝓸𝓿𝓲𝓭𝓮𝓼 𝓽𝓱𝓮 𝓸𝓷𝓵𝔂 𝓻𝓮𝓪𝓵
𝓹𝓵𝓮𝓪𝓼𝓾𝓻𝓮 𝓲𝓷 𝓵𝓲𝓯𝓮