codeforces 335A Banana(贪心)

时间:2021-11-08 10:34:42

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