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$,如果至少满足以下条件之一: 字符串长度 ...
随机推荐
- 数据库课程设计 PHP web实现
纪念一下自己写的东西.. 都说很垃圾就是了 直接用XAMPP做的 菜鸟网上学的PHP和HTML <!DOCTYPE html> <html> <head> < ...
- 在window下搭建即时即用的hyperledger fabric 的环境
有版本号的严格按要求,遇到不少坑 1)安装git 版本无要求 2)安装go 1.9 配置环境变量 3)安装Vagrant 1.9.4 4)安装VirtualBox 5.1.28 5)在go ...
- gcc 变量类型大小 练习 远离 cygwin64 需要带dll
/* testmini.c -- very simple test program for the miniLZO library */ #include <stdio.h> #inclu ...
- HTML页面中解决内容元素随窗口变化布局变乱问题
1.给body加上一个min-width最小宽度,以px为单位,这样当页面变小时,当达到你所设置的最小宽度,body的宽度不再改变,超出的部分会用横向滚动条显示,其内所有元素的布局也不会受影响. 2. ...
- 10MongoDB
一. 介绍MongoDB 1. NoSQL 1)“NoSQL”⼀词最早于1998年被⽤于⼀个轻量级的关系数据库的名字 随着web2.0的快速发展, NoSQL概念在2009年被提了出来 2)NoSQ ...
- hdu3404 Switch lights
题目描述 题解: 首先,由$SG$定理得SG(x,y)=mex(SG(x',y)^SG(x,y')^SG(x',y'))(x'<x,y'<y) 这里的$SG(x,y)$叫$Nim$积. $ ...
- dotTrace激活服务器
http://active.09l.me IntelliJ IDEA 7.0 或 更高DataGrip 1.0或更高ReSharper 3.1 或更高ReSharper Cpp 1.0 或更高dotT ...
- blocking and nonblocking assign
key word: 仿真建模 clock采样block/nonblock blocking时,有时候clk会sample edge后的data: nobocking时,clk sample 以前的d ...
- pandas.read_csv 报ssl.SSLError
- tornado框架基础06-SQLAlchemy连接数据库
01 ORM 在服务器后台,数据是要存储在数据库的,但是如果项目在开发和部署的时候,是使用的不同的数据库,该怎么办呢?是不是需要把所有的 SQL 语句都再重新写一遍呢? 和数据库相关,不同的数据库需要 ...