【题目链接】: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;
}

【codeforces 335A】Banana的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 546A】Soldier and Bananas

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. ubuntu 12.04下安装Qt出现cannot execute binary file的解决方案

    最近在ubuntu 12.04下安装QT的过程中,遇到一个问题. ./qt-opensource-linux-x64-5.7.0.run出现了bash: ./qt-opensource-linux-x ...

  2. 解决utf8' codec can't decode byte 0xe5 in position 0: unexpected end of data

    使用unicode对象的话,除了这样使用u标记,还可以使用unicode类以及字符串的encode和decode方法. unicode类的构造函数接受一个字符串参数和一个编码参数,将字符串封装为一个u ...

  3. 2.安装Cython

    许多科学的Python发行版,例如Anaconda,Enthought Canopy和Sage,捆绑Cython并且不需要设置. 与大多数Python软件不同,Cython需要在系统上存在C编译器.获 ...

  4. java静态方法

    静态方法(全局方法)不能访问this(当前对象)它和类没有关系,会有逻辑错误,当调用静态方法的时候不需要创建对象 可以直接为boolean result = MyTest.isPrime();直接访问 ...

  5. Jquery学习总结(4)——高效Web开发的10个jQuery代码片段

    在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...

  6. ActiveMQ学习总结(4)——业界消息队列简介

    最近开发公司的短信平台,要用到消息队列,之前用的是亚马逊的SQS,考虑到后续业务发展,对消息推送的高并发要求,公司决定采用RabbitMQ来替换.借此机会开始熟悉各种MQ产品,下面先给大家简介下业界常 ...

  7. java整型byte,short,int,long取值范围大小

     byte 1个字节 short 2个字节 int 4个字节long 8 个字节 varchar 可变长度的非Unicode数据,最长为8000个字符nvarchar 可变长度Unicode数据,最长 ...

  8. 记一次在广播(BroadcastReceiver)或服务(Service)里弹窗的“完美”实践

    事情是这样的,目前在做一个医疗项目,需要定时在某个时间段比如午休时间和晚上让我们的App休眠,那么这个时候在休眠时间段如果用户按了电源键点亮屏幕了,我们就需要弹出一个全屏的窗口去做一个人性化的提示,“ ...

  9. maven这些工具负责创建项目,然后maven负责打包好war包扔进tomcat容器,tomcat容器接受的只是jar包

    maven这些工具负责创建项目,然后maven负责打包好war包扔进tomcat容器,tomcat容器接受的只是jar包 2.tomcat不管你什么编译的,也不管你开发工具是什么.Tomcat只接受w ...

  10. poj2385(dp)

    题目链接:http://poj.org/problem?id=2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total S ...