天天看點

codeforces A. k-String 題解

a string is called a k-string if it can be represented as k concatenated

copies of some string. for example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string,

a 5-string, or a 6-string and so on. obviously any string is a 1-string.

you are given a string s, consisting of lowercase english letters and a positive integer k.

your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.

input

the first input line contains integer k (1?≤?k?≤?1000).

the second line contains s, all characters in s are

lowercase english letters. the string length s satisfies the inequality 1?≤?|s|?≤?1000,

where |s| is the length of string s.

output

rearrange the letters in string s in such a way that the result is a k-string.

print the result on a single output line. if there are multiple solutions, print any of them.

if the solution doesn‘t exist, print "-1" (without quotes).

sample test(s)

本題有意思,是hash表的靈活運用。

思路:

1 計算好總字元數,和使用hash表a[26]記錄好各個字元出現的次數

2 判斷總字元是否可以被k整除,如果不可以,那麼就不能分成k個子字元了

3 計算各個字元出現的次數是否能被k整除,如果不能,那麼就不能分成k個子字元

4 根據字元出現的次數逐個列印

繼續閱讀