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)=( ...
随机推荐
- spring mvc 配置运行报错误
四月 06, 2015 10:51:18 上午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server version: Ap ...
- [转]c++应用程序文件的编译过程
原文地址 这里讲下C++文件的编译过程及其中模板的编译过程: 一:一般的C++应用程序的编译过程. 一般说来,C++应用程序的编译过程分为三个阶段.模板也是一样的. 在cpp文件中展开inclu ...
- Tensorflow入门-上
前置准备 在阅读本文之前,请确定你已经了解神经网络的基本结构以及前向传播.后向传播的基本原理,如果尚未了解,可以查看下文. 深度学习之神经网络 什么是TensorFlow? TensorFlow是Go ...
- 《Java程序设计》课程试题
< Java程序设计 >课程试题 一.单项选择题(20题:每题2分,共40分) 1.若数组a定义为int[][]a=new int[3][4],则a是___. A)一维数组 B)二维数组 ...
- webstorm下开发微信小程序
- hdfs深入:02、今日课程内容大纲以及hdfs的基本实现
1.hadoop第三天课程内容 hdfs:分布式文件存储系统hdfs的架构图hdfs的副本机制以及block块hdfs的元数据信息fsimage与editshdfs的文件读写过程hdfs的javaAP ...
- POJ 1149 PIGS 建图,最大流
题意: 你m个猪圈以及每个猪圈里原来有多少头猪,先后给你n个人,每个人能打开某一些猪圈并且他们最多想买Ki头猪,在每一个人买完后能将打开的猪圈中的猪顺意分配在这次打开猪圈里,在下一个人来之前 已打开的 ...
- Jdk动态代理和CGLIB动态代理大比拼
前言: 这2种动态代理算是老生常谈的吧,面试还是会经常问到的,下面做下分析: jdk动态代理: import java.lang.reflect.InvocationHandler; import j ...
- 常见的Redis问题?
Redis的那些最常见面试问题[转] 1.什么是redis? Redis 是一个基于内存的高性能key-value数据库. 2.Reids的特点 Redis本质上是一个Key-Value类型的内存数据 ...
- 第一节:python提取PDF文档中的图片
由于项目需要将PDF文档当中的图片转换成图片,所以参考了这篇文章https://blog.csdn.net/qq_15969343/article/details/81673302后项目得以解决. 1 ...