CodeForces 288A Polo the Penguin and Strings (水题)

题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小。

析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候。

代码如下:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 10000 + 5;
const int INF = 0x3f3f3f3f;
const int dr[] = {0, 0, 1, -1};
const int dc[] = {1, -1, 0, 0};
int a[maxn];
vector<int> ans;

int main(){
    int n, m, d, k;
    scanf("%d %d", &n, &k);
    if(n < k){
        printf("-1
");
        return 0;
    }
    if(k == 1){
        if(n > 1){
            printf("-1
");
        }
        else printf("a");
        return 0;
    }
    for(int i = 0; i < n-k+2; ++i){
        if(i % 2)  ans.push_back(1);
        else ans.push_back(0);
    }
    for(int i = n-k+2; i < n; ++i)
        ans.push_back(i-n+k);
    for(int i = 0; i < n; ++i)
        printf("%c", ans[i] + 'a');
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5705423.html