【题目链接】: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. code-reading-notes--xml 解析

  2. libvirtd.service

    [root@kvm-server ~]# systemctl status libvirtd.service ● libvirtd.service - Virtualization daemon Lo ...

  3. 利用Python对文件进行批量重命名

    最近几天工作的内容是对40个项目进行考核,每个项目都需要一个考核评分表,已经有了项目的列表. 如果用常规的方法,需要复制40个文件,并逐个修改,不光工作量大,也容易出错,后期修改也不方便. 于是想到了 ...

  4. linux 上安装 redis

    一.安装gcc Redis是c语言开发的. 安装 redis 需要 c 语言的编译环境.如果没有 gcc 需要在线安装. yum install gcc-c++ 二.下载 redis 链接:https ...

  5. 2015 Multi-University Training Contest 4 hdu 5334 Virtual Participation

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  6. POJ——T 3159 Candies

    http://poj.org/problem?id=3159 Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 33328   ...

  7. objective-c訪问控制符

    objective-c中成员变量的四个訪问控制符: @private:仅仅有当前类的内部才干訪问 @public:全部人都可訪问 @protected:仅仅限当前类和它的子类可以訪问 @package ...

  8. 一步一步跟我学习hadoop(7)----hadoop连接mysql数据库运行数据读写数据库操作

        为了方便 MapReduce 直接訪问关系型数据库(Mysql,Oracle).Hadoop提供了DBInputFormat和DBOutputFormat两个类.通过DBInputFormat ...

  9. 关于Javakeywordsynchronized——单例模式的思考

    精彩的设计模式盛宴刚刚落下帷幕.三天的时间.真是学习到了非常多.当中,遗留的非常多的问题.今天就谈谈synchronized这个keyword.关于对synchronizedkeyword的思考是从单 ...

  10. Eclipse 更新Android SDK后,新建项目出现appcompat_v7project的相关问题

    Eclipse 更新Android SDK后,新建项目出现各种问题.网上各种解决方式,搞了好久,总结一下. 1.出现error: Error retrieving parent for item: N ...