【Polo the Penguin and Strings】【CodeForces - 288A 】(簡單尋找規律)
題目:
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 as s = 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 x
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;
}
}
}