题干:
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 ofnlowercase English letters (that is, the string's length equalsn), exactlykof these letters are distinct.
- No two neighbouring letters of a string coincide; that is, if we represent a string ass=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.
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 4
Output
ababacd
Input
4 7
Output
-1
题目大意:(摘抄)
给定一个长度为n的字符串,让你用前k种小写字母将字符串排成一个长度为n的,左右相邻字母不相同的,且字典序最小的字符串。注意必须要用K种。如果不能做到则输出-1.
解题报告:
水题,,但是还是有些粗心的,,k==1的时候也需要特判啊!!
AC代码:
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 ;
}