【题目链接】:http://codeforces.com/contest/335/problem/A

【题意】



让你构造一个长度为n的字符串;

每次你可以从这个字符串中任意取走字符;

让你求出取的次数最少的字符串,使得取这么多次能够构成所给的字符串s;

输出次数以及这个字符串;

【题解】



从小到大枚举取的次数;

显然取的次数越多,字符串的长度会越短;

根据每个字符出现的次数i;

能够得到这个字符串每个字符y最少应该出现多少次x;

x=(cnt[y]-1)/i + 1;

累加起来;

看看是不是小于等于n;

是的话,把每个字符应该出现的最小次数加进去;剩余的位置随便填’a’就好



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100; int num[300],n;
char s[N]; int main(){
//Open();
Close();
cin >> (s+1);
cin >> n;
rep1(i,1,(int) strlen(s+1)){
num[s[i]]++;
}
rep1(i,1,1000){
int now = 0;
for (char t = 'a';t<='z';t++)
if (num[t]){
now+=(num[t]-1)/i + 1;
}
if (now <=n){
cout << i << endl;
string s="";
for (char t = 'a';t<='z';t++)
if (num[t]){
int k = (num[t]-1)/i+1;
rep1(j,1,k) s+=t;
}
while ((int) s.size()<n) s+='a';
cout << s << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}

【codeforces 335A】Banana的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 546A】Soldier and Bananas

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. Redis:基础知识及其常用数据类型和关键字

    Redis: Redis是什么: REmote DIctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(Key-Value)分布式内存数 ...

  2. static方法调用

    Static方法调用,类名.方法名 int number = Integer.ParseInt(String ); 将字符串参数作为有符号的十进制整数进行解析 将数字解析成字节数组 Character ...

  3. angular-应用

    什么是SPA 真正的 AngularJS 单页 Web 应用(single page web application,SPA) 一些基础概念 <html> 元素是 AngularJS 应用 ...

  4. Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字

    题目链接:点击打开链接 每一个点都是最大值,把一整个序列和都压缩在一个点里. 1.普通的区间求和就是维护2个值,区间和Sum和延迟标志Lazy 2.Old 是该区间里出现过最大的Sum, Oldlaz ...

  5. Ubuntu: GlusterFS+HBase安装教程

    HBase通常安装在Hadoop HDFS上,但也能够安装在其它实现了Hadoop文件接口的分布式文件系统上.如KFS. glusterfs是一个集群文件系统可扩展到几peta-bytes. 它集合了 ...

  6. nodejs即时聊天

    一直想做一个即时聊天的应用,前几天看到了socket.io,感觉还不错.自己略加改动,感觉挺不错的.官网上给的样例非常easy,以下改进了一点,实现了历史消息的推送. demo地址:chat.code ...

  7. Gym 100733J Summer Wars 题解:灵活运用扫描线的思想

    题意: 给你n个点,m个横着的线段.你能够横移这些线段,可是这些线段的相对位置不能改变.假设一个点,在它的正上方和和正下方都有线段(包含线段的终点).则这个点被视为被"屏蔽".问通 ...

  8. html 标签: image也能提交form!!!

    html 标签: image也能提交form! !! image也能提交form 先前常常使用"<input type="submit" value="i ...

  9. UVA - 10229 Modular Fibonacci 矩阵快速幂

                                 Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...

  10. node16---cookie session

    03.js var express = require("express"); var app = express(); var db = require("./mode ...