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)

input

2
aazz

output

azaz

input

3
abcabcabz

output

-1

本题有意思,是hash表的灵活运用。

思路:

1 计算好总字符数,和使用hash表A[26]记录好各个字符出现的次数

2 判断总字符是否可以被k整除,如果不可以,那么就不能分成k个子字符了

3 计算各个字符出现的次数是否能被k整除,如果不能,那么就不能分成k个子字符

4 根据字符出现的次数逐个打印

#include <string>
#include <iostream>
using namespace std;

void KString()
{
	int k, len = 0;
	cin>>k;
	int A[26] = {0};
	char a;
	while (cin>>a)
	{
		A[a-‘a‘]++;
		len++;
	}
	if (len == 0 || len % k)
	{
		cout<<-1;
		return;
	}

	bool ks = true;
	for (unsigned i = 0; i < 26; i++)
	{
		if (A[i] % k != 0) ks = false;
	}
	if (!ks) cout<<-1;
	else
	{
		for (int d = 0; d < k; d++)
		{
			for (unsigned i = 0; i < 26; i++)
			{
				if (A[i])
					for (unsigned j = 0; j < A[i]/k; j++)
					{
						cout<<char(i + ‘a‘);
					}
			}
		}
	}
}

codeforces A. k-String 题解

时间: 2024-10-27 03:10:09

codeforces A. k-String 题解的相关文章

codeforces New Year Present 题解

The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception. Vasily knows that the best present is (no, it's not a contest) money. He's put n empty wallets from left to right in a r

codeforces A. Supercentral Point 题解

One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points(x1,?y1),?(x2,?y2),?...,?(xn,?yn). Let's define neighbors for some fixed point from the given set (x,?y): point (x',?y') is (x,?y)'s right neighbor,

codeforces Sereja and Dima 题解

Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take o

【codeforces】【比赛题解】#915 Educational CF Round 36

虽然最近打了很多场CF,也涨了很多分,但是好久没写CF的题解了. 前几次刚刚紫名的CF,太伤感情了,一下子就掉下来了,不懂你们Div.1. 珂学的那场我只做了第一题--悲伤. 这次的Educational Round打的还可以,虽然吧没有涨分(因为我是紫色的啊). 做了前4题,后面3题也比较简单,陆续也做完了. 所以心情好,来写一篇题解! [A]花园 题意: 长度为\(k\)的线段,用若干个长度为\(a_i\)的线段,正好覆盖.(\(a_i|k\)) 给定\(n\)个\(a_i\),求出最小的\

Educational Codeforces Round 64部分题解

Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点个数是否有限,如果有限,输出. 很明显当正方形套三角形或者三角形套正方形是交点个数是无限的(因为有一条边相交) 其他图形的嵌套交点个数比较好判断,不多赘述 但是注意坑点: 当按照矩形,园,三角这样的顺序是,三角与圆的一个交点是与圆和正方形的交点重合的,判一下就好了 #include<cstdio>

Codeforces 7E - Defining Macros 题解

目录 Codeforces 7E - Defining Macros 题解 前言 做法 程序 结尾 Codeforces 7E - Defining Macros 题解 前言 开始使用博客园了,很想写点东西.(逃 这是一道Codeforces题目. 做法 一道大模拟 相信大家都看得懂题目意思,我就不赘述了. 首先我们读进来\(n\)行,对于每一行,我们把它初步分成一下四块内容: #号 define 宏的名字 宏的表达式 注意:#和define中间可能会有空格,define和宏的名字,宏的名字和宏

codeforces D. Ice Sculptures 题解

The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus n ice sculptures were erected. The sculptures

Codeforces 827E Rusty String - 快速傅里叶变换 - 暴力

Grigory loves strings. Recently he found a metal strip on a loft. The strip had length n and consisted of letters "V" and "K". Unfortunately, rust has eaten some of the letters so that it's now impossible to understand which letter was

codeforces 219a k重复字符串。

背景:周赛d题,当时没读题,怎么知道其实有点水的,也是自己codeforces第一题吧,仰慕torist神牛.! #include<stdio.h> #include<string.h> int a[26],n=0; char b[1009]; bool ok(void); bool ok(void) { for(int j=0;j<26;j++) if(a[j]%n!=0) return false; return true; } int main(void) { whil

codeforces 128 B. String 优先队列

链接:http://codeforces.com/contest/128/problem/B B. String time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output One day in the IT lesson Anna and Maria learned about the lexicographic order. Stri