A. Polo the Penguin and Strings
2 seconds
256 megabytes
standard input
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:
- The string consists of n lowercase English letters (that is, the string's length equals n),
exactly k of these letters are distinct. - 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). - 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.
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.
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
7 4
ababacd
4 7
-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的更多相关文章
- 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 ...
- CodeForces 288A Polo the Penguin and Strings (水题)
题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小. 析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候. 代码如下: # ...
- CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...
- CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)
题意:给定 n 和k,n 表示有n个房子,然后每个有一个编号,一只鹅要从一个房间中开始走,下一站就是房间的编号,现在要你求出有多少种方法编号并满足下面的要求: 1.如果从1-k房间开始走,一定能直到 ...
- CodeForces 289B Polo the Penguin and Matrix (数学,中位数)
题意:给定 n * m 个数,然后每次只能把其中一个数减少d, 问你能不能最后所有的数相等. 析:很简单么,首先这个矩阵没什么用,用一维的存,然后找那个中位数即可,如果所有的数减去中位数,都能整除d, ...
- CodeForces 289A Polo the Penguin and Segments (水题)
题意:给你 n 段区间,而且还是不相交的,然后你只能向左扩展左端点,或者向右扩展右端点,然后扩展最少的步数让整数总数能够整除 k. 析:很简单么,只要在记录算一下数量,然后再算出 k 的倍数差多少就行 ...
- codeforces B. Polo the Penguin and Matrix 解题报告
题目链接:http://codeforces.com/problemset/problem/289/B 题目意思:给出一个 n 行 m 列的矩阵和数值 d .通过对矩阵里面的数进行 + d 或者 - ...
- codechef Polo the Penguin and the Tree
一般xor 的题目都是用trie解决. 那这道题是在树上的trie; 首先:从root==1,遍历树得到1到所有节点的xor 值. 然后对于每个点我们把其插入二进制树中. 对于每一个点查找其二进值异或 ...
- Codeforces Round #177 (Div. 2) B. Polo the Penguin and Matrix (贪心,数学)
题意:给你一个\(n\)x\(m\)的矩阵,可以对矩阵的所有元素进行\(\pm d\),问能否使得所有元素相等. 题解:我们可以直接记录一个\(n*m\)的数组存入所有数,所以\((a_1+xd)=( ...
随机推荐
- CSS中的趣事之float浮动
浮动float一般跟left或是right: 特性: 1,包裹性:浮动文本类型时,需要指定宽度width,如果不指定,就会折叠到最小宽度: 2,浮动会影响别的元素: 3,子级浮动,会导致父级高度 ...
- Proc datasets
作用:控制数据集.Datasets 过程运行结果不输出,结果只有在日志里才能看到. 基本语法: proc datasets lib=work; quit; 用法: 1. 更改数据集 proc data ...
- WOJ600——水题
第一次做本校OJ的题,被坑的好惨啊! 题目:600.Minimum Distance 题目大意:给定平面上3个点A.B.C,求平面上的任一顶点P,使得|PA|+2|PB|+3|PC|. 由于刚好在这 ...
- autorun - 自动装载/卸载CDROMs并在装载后执行/path/to/cdrom/autorun
总览 autorun [-lmqv?V] [-a EXEC] [-c CDPLAYER] [-e STRING] [-i MILLISEC] [-n STRING] [-t STRING] [--au ...
- java_线程的通信
线程的通信共有三个方法: wait()运行时阻塞,释放锁 notify()唤醒阻塞线程 notifll()唤醒全部阻塞线程 public class ThreadTest01 { public sta ...
- Linux下MySQL 5.7的初始化
要用管理员账号运行. systemctl start mysql#启动MySQL服务 mysqld_safe --user=mysql &#启动MySQL服务(安全方式) mysql -u r ...
- [Python3网络爬虫开发实战] 1.8.3-Scrapy-Splash的安装
Scrapy-Splash是一个Scrapy中支持JavaScript渲染的工具,本节来介绍它的安装方式. Scrapy-Splash的安装分为两部分.一个是Splash服务的安装,具体是通过Dock ...
- (八)python3 迭代
迭代:如果给定一个 list 或 tuple,我们可以通过 for 循环来遍历这个 list 或tuple,这种遍历我们称为迭代(Iteration) 字典: >>> d = {'a ...
- python while、continue、break
while循环实现用户登录 _user = "tom" _passwd = "abc123" counter = 0 while counter < 3: ...
- UVA 1596 Bug Hunt (大模拟 栈)
题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...