CF219A k-String(数学分析)

简介: 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;
  }
}


相关文章
|
NoSQL Linux Redis
Linux系统中安装redis+redis后台启动+常见相关配置
Linux系统中安装redis+redis后台启动+常见相关配置
|
11月前
|
存储 数据库 数据安全/隐私保护
互联网医院系统开发中常见的技术难点与解决思路
本文从技术视角剖析互联网医院系统五大核心难点:问诊状态机设计、音视频与业务解耦、电子处方规则动态化、医疗数据分级脱敏、支付幂等性保障,并给出可落地的状态枚举、策略模式、字段脱敏、回调校验等实战方案,助力构建高可用、强合规的数字医疗系统。(239字)
|
存储 缓存 小程序
微信小程序数据缓存与本地存储:优化用户体验
本文深入探讨微信小程序的数据缓存与本地存储,介绍其意义、机制及应用场景。通过合理使用内存和本地缓存,可减少网络请求、提升加载速度和用户体验。文中详细讲解了常用缓存API的使用方法,并通过一个新闻列表案例展示了缓存的实际应用。最后提醒开发者注意缓存大小限制、时效性和清理,以确保最佳性能。
|
存储 安全 Java
深入剖析Java并发库:Exchanger的工作原理与应用场景
深入剖析Java并发库:Exchanger的工作原理与应用场景
|
C++
C++语言基础 例程 调用构造函数和析构函数的顺序
贺老师的教学链接  本课讲解 析构函数应用实例方案1 const int N =500; class Salary { public: Salary(int); void input( ); void show( ); ~Salary(); private: double salarys[N]; int number; //实际人数 }; 方案
1165 0
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8077 2
|
11天前
|
域名解析 人工智能 Linux