Proble J Codeforces Round #135 (Div. 2) A. k-String

A. k-String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 sin 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)
input
2
aazz
output
azaz
input
3
abcabcabz
output
-1
 
   
ps:感谢莫莫的解题报告

这个题目主要是需要理解题意,静下心来看下,其实很简单。

题意:

     题目给一个K,给定一个字符串,里面包含若干字母。如果字符串可以用里面所有的字母重新组合为K-string,输出重组以后的字符串,否则输出-1K-string即为一个字符串可以分为一个子串复制K倍然后得到字符串。

解题思路:

     先判断是不是K-string,用一个数组保存从a~z出现的次数。如果某一个次数不是K的倍数,那么便直接输出-1即可。否则则将里面的次数全部除以K,用来输出。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
char a[1004],c[1004];             //a是输入的字符串,c则是需要连续输出K倍的子串
int b[26];                       //保存a~z字母出现额次数

int main()
{
    int i,j,k;
    while(cin>>k)
    {
      memset(b,0,sizeof(b));
      cin>>a;
      for(i=0;i<strlen(a);i++)
          b[a[i]-'a']++;
      int flag=0;
      for(i=0;i<26;i++)
          if(b[i]%k!=0)                      //不满足次数是K的倍数,跳出,输出-1
          {
             flag=1;            
               break;
          }
      if(flag)
          cout<<"-1"<<endl;
       else
       {
          int ll=-1;
          for(i=0;i<26;i++)
              if(b[i])
              {
                 int x=b[i]/k;                  //次数除以K
                 for(j=0;j<x;j++)
                     c[++ll]=i+'a';                 //用c来保存需要输出的子串
              }
          c[++ll]='\0';
          for(i=0;i<k;i++)                   //输出重组以后的K-string
              cout<<c;
          cout<<endl;
       }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值