【CodeForces - 289C】Polo the Penguin and Strings (水题,字符串,思维构造,有坑)
題干:
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:
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).
Examples
Input
7 4Output
ababacdInput
4 7Output
-1題目大意:(摘抄)
給定一個長度為n的字符串,讓你用前k種小寫字母將字符串排成一個長度為n的,左右相鄰字母不相同的,且字典序最小的字符串。注意必須要用K種。如果不能做到則輸出-1.
解題報告:
? ?水題,,但是還是有些粗心的,,k==1的時候也需要特判啊!!
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 1e6 + 5; int n,k; char s[MAX]; int main() {cin>>n>>k;if(k>n) {puts("-1");return 0 ;}if(n == 1) {printf("a");return 0 ;}if(n>1 && k==1) {puts("-1");return 0 ;}int tmp = k-2;int tt=tmp+2;for(int i = n; i>=n-tmp+1; i--) {s[i] = 'a'-1 + tt;tt--;}for(int i = 1; i<=n-tmp; i++) {if(i%2==1) s[i] = 'a';else s[i]='b';}printf("%s\n",s+1);return 0 ;}?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 289C】Polo the Penguin and Strings (水题,字符串,思维构造,有坑)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我国养老金将迎来第17连涨!过去15年年
- 下一篇: 2021年黄金还会涨吗?2021年黄金走