当前位置: 代码迷 >> 综合 >> Educational Codeforces Round 107 (Rated for Div. 2)——D. Min Cost String
  详细解决方案

Educational Codeforces Round 107 (Rated for Div. 2)——D. Min Cost String

热度:21   发布时间:2023-11-21 17:19:59.0

D. Min Cost String
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s define the cost of a string s as the number of index pairs i and j (1≤i<j<|s|) such that si=sj and si+1=sj+1.

You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost — find any of them.

Input
The only line contains two integers n and k (1≤n≤2?105;1≤k≤26).

Output
Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings — print any of them.

Examples
inputCopy
9 4
outputCopy
aabacadbb
inputCopy
5 1
outputCopy
aaaaa
inputCopy
10 26
outputCopy
codeforces

思路:

只要任意相邻的相邻两个不重复(不能连续三个相同)和相隔一个的两个(每两对要换一对)不重复就可以让相隔任意的不重复,把这个不重复的周期拉到最大,使尽量相同的多。即aabacad,其中aaa中换一个,同时axay的xy不同。最方便的就是aabacad bbcbd ccd d
为什么不是aabacad bbabcbd ccacbcd ,因为不重复不能有接轨,这样子ab 又反复出现很多次了

呸我规律没说对

是取2个,代码只是模拟这个

实际上规律是:
只要相邻的两个不重复,就不会有值同时使s[i]=s[j]且s[i+1]=s[j+1]

类似的,只要相邻的n个不重复,就不会有值同时使s[i]=s[j]且s[i+1]=s[j+1]且…且s[i+n-1]=s[j+n-1]

还有,会长小哥教我仔细分析样例,尤其是最值的这种qwq
小哥yyds
下面的代码是小哥的,30行ACdiv2D题绝了

#include<bits/stdc++.h>
using namespace std;
int n,k,tot=0;
char ch[27];
char ans[300005];
int main()
{
    scanf("%d%d",&n,&k);for(int i=1;i<=26;i++)ch[i]='a'+(i-1);for(int i=1;i<=k;i++){
    for(int j=i;j<=k-1;j++){
    tot++;ans[tot]=ch[j];tot++;ans[tot]=ch[i];}tot++;ans[tot]=ch[k];}for(int i=1;i<=n;i++){
    int p=i%tot;if(p==0)p=tot;printf("%c",ans[p]);}	 return 0;
}
  相关解决方案