time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Little penguin Polo adores strings. But most of all he adores strings of length n.

One day he wanted to find a string that meets the following conditions:

  1. The string consists of n lowercase English letters (that is, the string's length equals n),
    exactly k of these letters are distinct.
  2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn,
    then the following inequality holds,si ≠ si + 1(1 ≤ i < n).
  3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.

Help him find such string or state that such string doesn't exist.

String x = x1x2... xp is lexicographically
less than string y = y1y2... yq,
if either p < q and x1 = y1, x2 = y2, ...
, xp = yp,
or there is such number r (r < p, r < q),
that x1 = y1, x2 = y2, ...
, xr = yr and xr + 1 < yr + 1.
The characters of the strings are compared by their ASCII codes.

Input

A single line contains two positive integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 26) —
the string's length and the number of distinct letters.

Output

In a single line print the required string. If there isn't such string, print "-1" (without the quotes).

Sample test(s)
input
7 4
output
ababacd
input
4 7
output
-1

解题说明:此题是一道典型的贪心问题,既要保证字符串中随意两个连续字符串不同,也要保证字典序最小,最简单的想法是仅仅用a,b交替。在最后补上其它字符串就可以。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include <algorithm>
#include<cstring>
#include<string>
using namespace std; int main()
{
int n, k, c, r, i;
scanf("%d%d", &n, &k);
if (k == n&&k <= 26)
{
c = 'a';
for (i = 0; i<k; i++)
{
printf("%c", c + i);
}
printf("\n");
}
else if (k>n || k == 1)
{
printf("-1\n");
}
else
{
for (i = 0; i<n - (k - 2); i++)
{
if (i % 2 == 0)
{
printf("a");
}
else
{
printf("b");
}
}
r = 2;
c = 'a';
for (i; i < n; i++, r++)
{
printf("%c", c + r);
}
printf("\n");
}
return 0;
}

A. Polo the Penguin and Strings的更多相关文章

  1. codeforces 288A:Polo the Penguin and Strings

    Description Little penguin Polo adores strings. But most of all he adores strings of length n. One d ...

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

    题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小. 析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候. 代码如下: # ...

  3. CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)

    题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...

  4. CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)

    题意:给定 n 和k,n 表示有n个房子,然后每个有一个编号,一只鹅要从一个房间中开始走,下一站就是房间的编号,现在要你求出有多少种方法编号并满足下面的要求: 1.如果从1-k房间开始走,一定能直到 ...

  5. CodeForces 289B Polo the Penguin and Matrix (数学,中位数)

    题意:给定 n * m 个数,然后每次只能把其中一个数减少d, 问你能不能最后所有的数相等. 析:很简单么,首先这个矩阵没什么用,用一维的存,然后找那个中位数即可,如果所有的数减去中位数,都能整除d, ...

  6. CodeForces 289A Polo the Penguin and Segments (水题)

    题意:给你 n 段区间,而且还是不相交的,然后你只能向左扩展左端点,或者向右扩展右端点,然后扩展最少的步数让整数总数能够整除 k. 析:很简单么,只要在记录算一下数量,然后再算出 k 的倍数差多少就行 ...

  7. codeforces B. Polo the Penguin and Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/289/B 题目意思:给出一个 n 行 m 列的矩阵和数值 d .通过对矩阵里面的数进行 + d 或者 - ...

  8. codechef Polo the Penguin and the Tree

    一般xor 的题目都是用trie解决. 那这道题是在树上的trie; 首先:从root==1,遍历树得到1到所有节点的xor 值. 然后对于每个点我们把其插入二进制树中. 对于每一个点查找其二进值异或 ...

  9. Codeforces Round #177 (Div. 2) B. Polo the Penguin and Matrix (贪心,数学)

    题意:给你一个\(n\)x\(m\)的矩阵,可以对矩阵的所有元素进行\(\pm d\),问能否使得所有元素相等. 题解:我们可以直接记录一个\(n*m\)的数组存入所有数,所以\((a_1+xd)=( ...

随机推荐

  1. scrapy增加爬取效率

    增加并发: 默认scrapy开启的并发线程为32个,可以适当进行增加.在settings配置文件中修改CONCURRENT_REQUESTS = 100值为100,并发设置成了为100. 降低日志级别 ...

  2. 火狐加载用户配置文件 "C:\XXX\Mozilla Firefox\firefox.exe" http://192.168.1.1:8080 -profile ../kkk

    "C:\XXX\Mozilla Firefox\firefox.exe" http://192.168.1.1:8080 -profile ../kkk $("#clic ...

  3. TYPE=MyISAM 与 ENGINE=MyISAM 的区别(摘要版)

    TYPE=MyISAM 和 ENGINE=MyISAM 都是设置数据库存储引擎的语句 (老版本的MySQL使用TYPE而不是ENGINE(例如,TYPE = MYISAM). MySQL 5.1为向下 ...

  4. CE工具里自带的学习工具--第一关

    点击[下一步],进入第二关

  5. Duplicate fragment name ERROR Jetty Maven Plugin

    http://stackoverflow.com/questions/5802096/duplicate-fragment-name-error-jetty-maven-plugin 4down vo ...

  6. ansible相关说明

    2.ansible相关说明 2.1.ansible相关命令 ansible:定义并运行简单任务,主要执行ad-hoc命令 ansible-config:查看.编辑.管理ansible配置 ansibl ...

  7. 查找BUG的方法

    1)测试环境 1)代码调试 2)问题重现 3)思考问题所在 2)生产环境 1)思考 2)测试本地环境是否存在问题 3)打开日志查看 4)思考是否是数据原因 5)拷贝数据到本地进行重现 3)未知错误 1 ...

  8. 解决WCF接口无法传递object参数的问题

    在某些场合中,我们需要提供以object为参数的方法.不过在WCF中,由于需要序列化与反序列化,因此它要求所有WCF传递的参数类型都是已知的,无法传递object这种未知类型.即使用了KnownTyp ...

  9. secureCRT 破解

    转自:http://www.cnblogs.com/qingtingzhe/articles/5008902.html

  10. python send email

    #!/usr/bin/python # -*- coding: UTF-8 -*- # coding:utf8 from smtplib import SMTP_SSL from email.head ...