【codeforces 335A】Banana

【题目链接】: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;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626262.html