CUIT ACM Personal Training 11.27(FM)F - Polo the Penguin and Strings

F - Polo the Penguin and Strings

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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).

Sample Input

Input
7 4
Output
ababacd
Input
4 7
Output
-1

題解:這個題的題目長度不短,具體講的是給你k個不同的字母,要你輸出n長度的字符串,要求:1.字符串字典序最小;2.字符串相鄰的字母不能相同,這個就是一個貪心算法。首先爲了字典序最小,所以k個字母肯定是從前開始的,選定了字母之後,又要相鄰的字符串不相同,那麼當k=1,也就是隻有1個字母的時候,除非n=1,只輸出一個a,否則就只能輸出-1。k!=1,時,當n<k,則表示用k個字母去組成一個比字母的個數還要少的字符串,這明顯是不成立的,所以輸出-1。n>k時,那麼就一直輸出ab,直到最後剩下k-2個位子放餘下的字母便行了。


AC代碼如下:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n, k, c, r, i;
    cin>>n>>k;
    if(k == 1 && n == 1){
        cout<<"a"<<endl;
        return 0;}
    if (k > n || (k == 1 && n != 1))
        printf("-1\n");
    else
    {
        for (i = 0; i<n - (k - 2); i++){
            if (i % 2 == 0) cout<<"a";
            else cout<<"b";}
        for (i=0; i<k-2; i++)
        {
            printf("%c",'c'+i);
        }
        cout<<endl;
    }
    return 0;
}


發佈了36 篇原創文章 · 獲贊 14 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章