CodeForces 288A--贪心
来源:互联网 发布:mac可以玩守望先锋吗 编辑:程序博客网 时间:2024/06/07 00:43
题意:
给定一个长度为n的字符串,让你用前k种小写字母将字符串排成一个长度为n的,左右相邻字母不相同的,且字典序最小的字符串。注
意必须要用K种。如果不能做到则输出-1.
输入:
7 4
4 7
输出:
ababacd
-1
分析:
此题是一道典型的贪心问题,既要保证字符串中任意两个连续字符串不同,也要保证字典序最小,最简单的想法是只用a,b交替,在最后补上其他字符串即可。当k=1或者n<k时,输出-1,一定要找准-1的条件是什么。
代码:
#include <bits/stdc++.h>using namespace std;int main(){ int n,m; cin >>n>>m; if (m==1&&n==1) { cout <<'a'; return 0; } if(m>n||m==1) { cout <<-1<<endl; return 0; } for (int i=1; i<=n-(m-2); i++) { if (i%2==1) cout <<'a'; else cout <<'b'; } for (int i=1; i<=m-2; i++) cout <<(char)(i+98); //从c开始 return 0;}
0 0
- CodeForces 288A--贪心
- A. Dragons codeforces+贪心
- Codeforces 556A 贪心
- Codeforces 412A Poster(贪心)
- CodeForces 230A Dragons(贪心)
- CodeForces 230A 贪心+排序
- Codeforces 484A Bits(贪心)
- Codeforces 484A - Bits (贪心)
- Codeforces 484A. Bits 贪心
- codeforces A. Combination Lock 贪心
- codeforces 651A Joysticks【贪心】
- Codeforces 651A Joysticks 【贪心】
- codeforces--651A Joysticks(贪心)
- Codeforces 651A Joysticks【贪心】
- CodeForces - 730A 贪心+模拟
- Codeforces 853A Planning 【贪心】
- Codeforces 902 A.Visiting a Friend 贪心
- 【codeforces #3】A——BFS+贪心
- 16标准(键盘,控制台)输入输出流
- 判断循环的用法 if switch
- 更正swiprefreshlayout主动显示小圆圈
- 【流媒體】 Android 实时视频编码—H.264硬编码
- Log4j 2使用教程
- CodeForces 288A--贪心
- debug log
- 杭电oj 1096 A+B for Input-Output Practice (VIII)
- Imgproc_4_图像轮廓
- 基本数据类型与引用数据类型参数传递
- iOS编程(双语版)-视图-Frame/Bounds/Center
- Centos7 下mysql安装配置
- codeforces Round 286# problem A. Mr. Kitayuta's Gift < 回文串 >
- 二叉树的一些笔试面试常见题目