【Polo the Penguin and Strings】【CodeForces - 288A 】(简单寻找规律)

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/84404971

题目:

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:

  1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
  2. 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).
  3. 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是字符串中出现的字母的次序(从a,b,c……),问怎么寻找最小字典序的字符串,根据题目样例,能够找到几个坑点,k=1是若n!=1 ,无解,k=0||k>26无解。

剩余的情况就是 将n-k+1的前边用ab组成填满,然后后边的用cdef……升序组成。

ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxn =1e6+100;

char str[maxn];

int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		if(n<k||k<=0||k>26)
		{
			printf("-1\n");
			continue;
		}
		if(n==1&&k==1)
		{
			printf("a\n");
			continue;
		}
		else if(k==1&&n!=1)
		{
			printf("-1\n");
			continue;
		}
		else
		{
			str[0]='a',str[1]='b';
			int i;
			for(i=2;i<=n-k+1;i++)
			{
				if(i%2==0)
					str[i]='a';
				else
					str[i]='b';
			}
			for(int j=2;j<k;j++,i++)
			{
				str[i]='a'+j;
			}
			str[n]='\0';
			cout<<str<<endl;
		}
			
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/84404971