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 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 andx1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), thatx1 = 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).
Sample test(s)
input
7 4
output
ababacd
input
4 7
output
-1
极为简单的水题:比赛的时候没出,还是因为审题+极端数据的问题。在n>1,并且是m==1时应该输出-1而我输出了N个a..
将思路和重要的条件全部列在纸上。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n,m,i,j,k;
while(~scanf("%d%d",&n,&m))
{
if(n<m)
{
printf("-1\n");
continue;
}
else if(m==1)
{
if(n>1)
{
printf("-1\n");
continue;
}
else
{
printf("a\n");
continue;
}
}
int tmp=m-2;
k=n-tmp;
if(k%2)
{
k/=2;
while(k--)
{
printf("ab");
}
printf("a");
}
else
{
k/=2;
while(k--)
{
printf("ab");
}
}
for(i=99; i<tmp+99; i++)
{
printf("%c",i);
}
printf("\n");
}
return 0;
}