转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

 

 Banana

Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.

Input

The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).

Output

On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.

Sample test(s)
input
banana
4
output
2
baan
input
banana
3
output
3
nab
input
banana
2
output
-1
Note

In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".

分析:贪心,先取一遍所有的字母,每次优先去当前所需最多的块数的字母。

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI; int num[];
char str[];
struct node{
int num,t;
char a;
node(int _num,char _a){
num=_num,a=_a,t=;
}
friend bool operator<(node y,node x){
return (y.num+y.t-)/y.t < ((x.num+x.t-)/x.t);
}
};
int main()
{
ios::sync_with_stdio(false);
string s;
int n;
cin>>s;
cin>>n;
int len=s.length();
int tot=;
for(int i=;i<len;i++)num[s[i]]++;
priority_queue<node> q;
int now=;
for(int i=;i<;i++){
if(num[i]){
str[now++]=i;
q.push(node(num[i],i));
}
}
if(now>n){
cout<<-<<endl;
return ;
}
for(;now<n;){
node p=q.top();
q.pop();
str[now++]=p.a;
p.t++;
q.push(p);
}
node p=q.top();
int ans=(p.num+p.t-)/p.t;
cout<<ans<<endl;
cout<<str<<endl; return ;
}

codeforces 335A Banana(贪心)的更多相关文章

  1. CodeForces - 158B.Taxi (贪心)

    CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...

  2. 【codeforces 335A】Banana

    [题目链接]:http://codeforces.com/contest/335/problem/A [题意] 让你构造一个长度为n的字符串; 每次你可以从这个字符串中任意取走字符; 让你求出取的次数 ...

  3. codeforces 724D(贪心)

    题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有 ...

  4. Codeforces 626G Raffles(贪心+线段树)

    G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...

  5. Cut 'em all! CodeForces - 982C(贪心dfs)

    K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...

  6. CodeForces - 940E - Cashback +贪心+DP

    传送门:CodeForces - 940E - Cashback 题意:在一个长度为n的数组中,可以分出长度为 k 连续的多个数组b(每个数组 b 的 k 可不相同),然后,可以对每个数组 b 进行删 ...

  7. Codeforces 515C 题解(贪心+数论)(思维题)

    题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...

  8. CodeForces 485C Bits[贪心 二进制]

    C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...

  9. codeforces 732E(贪心)

    题目链接:http://codeforces.com/contest/732/problem/E 题意:有n台计算机,m个插座,每台计算机有一个值a[i],每个插座有一个值b[i],每个插座最多只能对 ...

随机推荐

  1. 探索A@1db9742

    public class S { /**   * @param args   */ public static void main(String[] args) { System.out.printl ...

  2. SATA接口硬盘加密器

    加密卡置于主板与硬盘.光驱之间,透明实时地对写入数据进行加密,对读出数据进行解密,有效防止信息被窃.未经授权的阅读和修改,以及硬盘.光盘丢失.被盗.废弃.非法用户访问而引发的敏感信息泄密问题,为用户打 ...

  3. c#按键Up和Down对Textbox的内容加1减1

    private void textBox_proc1value_KeyDown(object sender, KeyEventArgs e) { Keys key = e.KeyCode; if (e ...

  4. hdu5322 Hope(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Hope Time Limit: 10000/5000 MS (Java/Othe ...

  5. web api简单验证实现办法

    需要使用WEBAPI,但是有验证问题没解决.后来参考网上文章做了一下DEMO 思路: 就是根据用户的账号在服务端加密一个字符串,然后返回给用户端. 具体: 一个用户编号用于唯一身份识别,密码,一个密钥 ...

  6. Pyhon之常用操作符 - 零基础入门学习Python006

    Pyhon之常用操作符 让编程改变世界 Change the world by program 今天我们来谈谈Python之常用操作符,在此之前的一些例子中,大家已经看到Python可以完成简单的算术 ...

  7. hdu 5305Friends

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 Problem Description There are n people and m pai ...

  8. c# 弹出框-后台调前台函数

    前台代码: <script src="../../../Common/Scripts/Order/popup.js" type="text/javascript&q ...

  9. ps存jpeg,格式保存的时候为什么选择“基线”

    jpeg是印前和网页设计常用的格式,最大好处就是能很大程度上压缩图像大小. 在ps中将图片保存为jpeg格式的时候会出现以下选项: 其中:图像选项都很熟悉,是关于图像质量的:而格式选项的用途主要是针对 ...

  10. 《Programming WPF》翻译 第6章 3.二进制资源

    原文:<Programming WPF>翻译 第6章 3.二进制资源 尽管ResourceDictionary和系统级别的资源适合于作为数据存在于对象中,然而,并不是所有的资源都能很好的满 ...