codeforces 335A Banana(贪心)

转载请注明出处: 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".

 

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

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 #include <iostream>
 6 #include <sstream>
 7 #include <ios>
 8 #include <iomanip>
 9 #include <functional>
10 #include <algorithm>
11 #include <vector>
12 #include <string>
13 #include <list>
14 #include <queue>
15 #include <deque>
16 #include <stack>
17 #include <set>
18 #include <map>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cmath>
22 #include <cstring>
23 #include <climits>
24 #include <cctype>
25 using namespace std;
26 #define XINF INT_MAX
27 #define INF 0x3FFFFFFF
28 #define MP(X,Y) make_pair(X,Y)
29 #define PB(X) push_back(X)
30 #define REP(X,N) for(int X=0;X<N;X++)
31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
33 #define CLR(A,X) memset(A,X,sizeof(A))
34 #define IT iterator
35 typedef long long ll;
36 typedef pair<int,int> PII;
37 typedef vector<PII> VII;
38 typedef vector<int> VI;
39 
40 int num[1010];
41 char str[1010];
42 struct node{
43     int num,t;
44     char a;
45     node(int _num,char _a){
46         num=_num,a=_a,t=1;
47     }
48     friend    bool operator<(node y,node x){
49         return (y.num+y.t-1)/y.t < ((x.num+x.t-1)/x.t);
50     }
51 };
52 int main()
53 {
54     ios::sync_with_stdio(false);
55     string s;
56     int n;
57     cin>>s;
58     cin>>n;
59     int len=s.length();
60     int tot=0;
61     for(int i=0;i<len;i++)num[s[i]]++;
62     priority_queue<node> q;
63     int now=0;
64     for(int i=0;i<1010;i++){
65         if(num[i]){
66             str[now++]=i;
67             q.push(node(num[i],i));
68         }
69     }
70     if(now>n){
71         cout<<-1<<endl;
72         return 0;
73     }
74     for(;now<n;){
75         node p=q.top();
76         q.pop();
77         str[now++]=p.a;
78         p.t++;
79         q.push(p);
80     }
81     node p=q.top();
82     int ans=(p.num+p.t-1)/p.t;
83     cout<<ans<<endl;
84     cout<<str<<endl;
85 
86     return 0;
87 }
View Code

 

转载于:https://www.cnblogs.com/fraud/p/4393659.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值