Reprint Please specify source: 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 have exactly one character printed on it, so a sheet of stickers can is described by a string of length < Span class= "Tex-span" > n . Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and could specify any string of Length N for The sheets, but all the sheets must was identical, so the string was the same for all sheets. Once She attains the sheets of stickers, she'll take a some of the stickers from the sheets and arrange (on any order) the M to Form s . Determine the minimum number of sheets she had to buy, and provide a string describing a possible sheet of stickers she sh Ould Buy.
Input
The first line contains string s (1≤| S| ≤1000), consisting of lowercase 中文版 characters only. The second line contains an integer n (1≤ n ≤1000).
Output
On the first line, print the minimum number of sheets piegirl have to buy. On the second line, print a string consisting of n lower case 中文版 characters. This string should describe a sheet of stickers, the 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".
Analysis: Greedy, first Take all the letters, each time priority to go to the current required maximum number of blocks of letters.
1 //#####################2 //Author:fraud3 //Blog:http://www.cnblogs.com/fraud/4 //#####################5#include <iostream>6#include <sstream>7#include <ios>8#include <iomanip>9#include <functional>Ten#include <algorithm> One#include <vector> A#include <string> -#include <list> -#include <queue> the#include <deque> -#include <stack> -#include <Set> -#include <map> +#include <cstdio> -#include <cstdlib> +#include <cmath> A#include <cstring> at#include <climits> -#include <cctype> - using namespacestd; - #defineXinf Int_max - #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) in #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) to #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) + #defineDEP (x,r,l) for (int x=r; x>=l; x--) - #defineCLR (a,x) memset (a,x,sizeof (A)) the #defineIT iterator *typedefLong Longll; $typedef pair<int,int>PII;Panax Notoginsengtypedef vector<pii>VII; -typedef vector<int>VI; the + intnum[1010]; A Charstr[1010]; the structnode{ + intnum,t; - CharA; $Nodeint_num,Char_a) { $num=_num,a=_a,t=1; - } -FriendBOOL operator<(node Y,node x) { the return(y.num+y.t-1)/y.t < (x.num+x.t-1)/x.t); - }Wuyi }; the intMain () - { WuIos::sync_with_stdio (false); - strings; About intN; $Cin>>s; -Cin>>N; - intlen=s.length (); - inttot=0; A for(intI=0; i<len;i++) num[s[i]]++; +Priority_queue<node>Q; the intnow=0; - for(intI=0;i<1010; i++){ $ if(Num[i]) { thestr[now++]=i; the Q.push (Node (num[i],i)); the } the } - if(now>N) { incout<<-1<<Endl; the return 0; the } About for(;now<N;) { theNode p=q.top (); the Q.pop (); thestr[now++]=P.a; +p.t++; - Q.push (p); the }BayiNode p=q.top (); the intAns= (p.num+p.t-1)/p.t; thecout<<ans<<Endl; -cout<<str<<Endl; - the return 0; the}
View Code
Codeforces 335A Banana (greedy)