Codeforces Round #135 (Div. 2)---A. k-String
2 seconds
256 megabytes
standard input
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.
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.
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).
2
aazz
azaz
3
abcabcabz
-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的更多相关文章
- 构造 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 ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- 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 ...
- 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'). ...
- Codeforces Round #550 (Div. 3) E. Median String (思维,模拟)
题意:给你两个字符串\(s\)和\(t\),保证\(t\)的字典序大于\(s\),求他们字典序中间的字符串. 题解:我们假设题目给的不是字符串,而是两个10禁止的正整数,那么输出他们之间的数只要把他两 ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- 【Codeforces Round #423 (Div. 2) C】String Reconstruction
[Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...
- Codeforces Round #656 (Div. 3) D. a-Good String
题目链接:https://codeforces.com/contest/1385/problem/D 题意 一个小写字母串称为 $c-good\ string$,如果至少满足以下条件之一: 字符串长度 ...
随机推荐
- docker-machine 快速搭建docker环境
环境:腾讯云测试成功 1.条件:本地主机A和远程主机B 2.远程主机B,配置免密登录 1,在本地主机A上生成公钥和私钥,生成命令:ssh-keygen -t rsa 私钥:id_rsa 公钥:id_r ...
- 基于flask的网页聊天室(四)
基于flask的网页聊天室(四) 前言 接前天的内容,今天完成了消息的处理 具体内容 上次使用了flask_login做用户登录,但是直接访问login_requare装饰的函数会报401错误,这里可 ...
- Python 模块学习(一)
一.时间模块 import time # 模块调用语句 注意:模块级导入一般放在文件顶部 import datetime print(time.time()) # 1550411181.441547: ...
- Druid配置
spring-mybatis.xml <!-- 定义数据源Bean --> <!-- Druid --> <bean id="dataSource" ...
- bootspring + mybaits +mysql Date 类型的处理
mysql 中有date 类型的属性,java实体类中对应的属性是java.sql.Date 类的. 最初的bug是怎么新增,joinDate 值都是null. 千辛万苦学会了用String转Date ...
- HR面试你需要注意什么?
公司的面试流程一般是笔试—>技术面试—>hr面试,在大部分应聘测试工程师这种技术岗的应聘者理解中,通常认为通过技术面试了,后面的hr面试基本就是走流程过形式.也正因如此,我们习惯性地把精力 ...
- mybatis插件 mybatis插件-------从dao快速定位到mapper的sql语句
步骤一:打开settings,点击plugins 快捷键ctrl+alt+s打开settings 步骤二.点击ClearCase Integration,并点击下面中间的按钮(browse repos ...
- Codeforces Round #211 (Div. 2)-D. Renting Bikes,二分!感谢队友出思路!
D. Renting Bikes 读懂题后一开始和队友都以为是贪心.可是贪心又怎么贪呢..我们无法确定到底能买多少车但肯定是最便宜的前x辆.除了公共预算每个人的钱只能自己用,也无法确定每个人买哪一辆车 ...
- codevs 1743 反转卡片 rope or splay
[codevs1743]反转卡片 题目描述 Description [dzy493941464|yywyzdzr原创] 小A将N张卡片整齐地排成一排,其中每张卡片上写了1~N的一个整数,每张卡片上的数 ...
- 路线统计(codevs 1482)
题目描述 Description N个节点的有向图, 求从start到finish刚好经过时间time的总方案数 mod 502630. 输入描述 Input Description 第一行包含一个整 ...