CF219A k-String(数学分析)

题目描述

A string is called a kk -string if it can be represented as kk 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 ss , consisting of lowercase English letters and a positive integer kk . Your task is to reorder the letters in the string ss in such a way that the resulting string is a kk -string.

输入格式

The first input line contains integer kk ( 1<=k<=10001<=k<=1000 ). The second line contains ss , all characters in ss are lowercase English letters. The string length ss satisfies the inequality 1<=|s|<=10001<=∣s∣<=1000 , where |s|∣s∣ is the length of string ss .

输出格式

Rearrange the letters in string ss in such a way that the result is a kk -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).

题意翻译

如果一个字符串中可以看做某个子串出现k次,那么它叫作  k−字符串.例如:字符串aabaabaabaab 是一个 1 −字符串、2- 字符串、4- 字符串(译者注:包含 aabaabaabaab 一次, aabaab两次,aab 四次)但显然它不是一个 3 −字符串、 5−字符串、 6−字符串等等。显然,任何字符串都是  1−字符串。

你将被给出一个字符串  s(仅含小写字母)和一个正整数 k 。你的任务是输出 s 重新排序后可满足的  k−字符串格式。

输入格式

第一行包含一个正整数k (1≤k≤1000)。 第二行包含一个字符串s , 所有字符均为小写字母。字符串 s的长度|s|满足 1≤∣s∣≤1000。

输出格式

重新排序字符串 s,使得它满足  k−字符串的格式。如果有多组答案,请输出所有答案。 如果答案不存在,输出 -1

输入输出样例

输入 

2
aazz

输出  

azaz

输入 

3
abcabcabz

输出  

-1

题目分析;首先我们分析可以发现如果每个字符的数除k==0我们才可以进行打印否则就是不能,思路就是这样,具体操作看代码。

#include<bits/stdc++.h>
using namespace std;
int n,cnt[101],tot[101];
string s;
signed main()
{
	cin>>n>>s;
	for(int i=0;i<s.length();i++)
		cnt[s[i]-'a']++;
	for(int i=0;i<=27;i++)
	{
		if(cnt[i]!=0&&cnt[i]%n!=0) {cout<<-1;return 0;}
		tot[i]=cnt[i]/n;
	}	
	while(1)
	{
		bool flag=0;
		for(int i=0;i<=27;i++)
			if(cnt[i]!=0) 
				for(int j=1;j<=tot[i];j++)
					cout<<char(i+'a'),cnt[i]--,flag=1;
		if(!flag) break;
	}
}

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法编程张老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值