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

解题思路:给一个串。问能否由k个同样的串连接而成。

用STL里的map。扫一遍。分别记录每一个字符的个数。在推断全部的字符是否是k的倍数,若不是,则输出-1;否则,遍历依次map。每一个字符输出(总个数)/k个,然后反复k次就可以。

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7fffffff map<char, int> m; int main()
{
#ifdef sxk
freopen("in.txt","r",stdin);
#endif
int n;
string s;
while(scanf("%d",&n)!=EOF)
{
cin>>s;
int len = s.size();
for(int i=0; i<len; i++)
m[s[i]] ++;
map<char, int>::iterator it;
int flag = 1;
for(it=m.begin(); it!=m.end(); it++){
if(it->second % n){
flag = 0;
break;
}
}
if(!flag) printf("-1\n");
else{
for(int j=0; j<n; j++){
for(it=m.begin(); it!=m.end(); it++){
for(int i=1; i<=it->second/n; i++)
printf("%c", it->first);
}
}
printf("\n");
}
}
return 0;
}

Codeforces Round #135 (Div. 2)---A. k-String的更多相关文章

  1. 构造 Codeforces Round #135 (Div. 2) B. Special Offer! Super Price 999 Bourles!

    题目传送门 /* 构造:从大到小构造,每一次都把最后不是9的变为9,p - p MOD 10^k - 1,直到小于最小值. 另外,最多len-1次循环 */ #include <cstdio&g ...

  2. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  3. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  4. 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

    题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...

  5. Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心

    D. Binary String Minimizing You are given a binary string of length n (i. e. a string consisting of ...

  6. Codeforces Round #598 (Div. 3) D. Binary String Minimizing

    You are given a binary string of length nn (i. e. a string consisting of nn characters '0' and '1'). ...

  7. Codeforces Round #550 (Div. 3) E. Median String (思维,模拟)

    题意:给你两个字符串\(s\)和\(t\),保证\(t\)的字典序大于\(s\),求他们字典序中间的字符串. 题解:我们假设题目给的不是字符串,而是两个10禁止的正整数,那么输出他们之间的数只要把他两 ...

  8. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  9. 【Codeforces Round #423 (Div. 2) C】String Reconstruction

    [Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...

  10. Codeforces Round #656 (Div. 3) D. a-Good String

    题目链接:https://codeforces.com/contest/1385/problem/D 题意 一个小写字母串称为 $c-good\ string$,如果至少满足以下条件之一: 字符串长度 ...

随机推荐

  1. 二分+贪心 || CodeForces 551C GukiZ hates Boxes

    N堆石头排成一列,每堆有Ai个石子.有M个学生来将所有石头搬走.一开始所有学生都在原点, 每秒钟每个学生都可以在原地搬走一块石头,或者向前移动一格距离,求搬走所有石头的最短时间. *解法:二分答案x( ...

  2. HDU6199 gems gems gems (DP)

    题意:有n颗石子 两个人轮流拿 如果上一个人拿了x颗 这个人就可以拿x或x+1颗 问先手能获得与后手的价值差最大是多少 题解:看起来是博弈 其实是DP dp[i][j][0/1]表示当前该0/1拿 拿 ...

  3. css 给div 添加滚动条样式hover 效果

             css .nui-scroll { margin-left: 100px; border: 1px solid #000; width: 200px; height: 100px; ...

  4. Yii1 获取当前请求的url

    echo Yii::app()->getRequest()->getUrl();

  5. Socket通信时服务端无响应,客户端超时设置

    背景:在写一个客户端的socket程序,服务端没有返回消息,客户端一直在等待. 目标:我需要设置一个时间,如果超过这个时间客户端自动断开连接.最好是在服务端实现,客户端对我来说不可控.

  6. IO之Object流举例

    import java.io.*; public class TestObjectIO { public static void main(String args[]) throws Exceptio ...

  7. Go:获取命令行参数

    一.Low B 方式 package main import ( "fmt" "os" ) func main() { fmt.Println("命令 ...

  8. JS打包与代码分割

    参考来源:https://github.com/ruanyf/webpack-demos#demo01-entry-file-source 后面的代码:https://github.com/94713 ...

  9. Linux文本检索命令grep笔记

    grep是在linux系统中基于行文本非常实用检索工具,通过该命令可以将匹配到的结果信息输出到终端控制台. 语法格式:grep [-ivnc] '需要匹配的内容' 文件名 常用参数说明: -i 检索的 ...

  10. python 学习总结2

    温度转换问题 一.温度转换 目前有两种表示温度的方法一种是摄氏度另一种是华氏度,摄氏度的结冰点为0度,沸点为100度将温度等分刻画,华氏度的结冰点为32度,沸点为212度将温度进行等刻度划分. 现需要 ...