SPOJ:PATHETIC STRINGS(分配问题&贪心)
Problem statement:
A string is said to be “PATHETIC” if all the characters in it are repeated the same number of times. You are given a string of length n, what is the minimum number of changes required to make a string “PATHETIC”. The string has only lower case letters and you can change any letter to any other letter.
Input Format
The first line contains an integer T, the number of test cases. This is followed by T test cases each containing 1 line:
Each testcase consists of a string composed of lowercase letters.
Output Format
For each testcase, print in a new line the minimum number of changes required.
Constraints
1 ≤ T ≤ 1370
1 ≤ n ≤ 1991
Sample Input :
2
bbaccaaa
ccaacb
Output:
2
1
题意:给定字符串,问这样才能让字符串里的每个字符的次数相同,每次可以把任意的字符变成任意的字符,求最小操作次数。
思路:把字符想成箱子,那么最多有26个箱子,枚举箱子数X,然后不难知道相应的变化数,更新最小值:
当前箱子大于X,那么从大到小排序,把X后面的几个箱子的货物搬出来,如果前面X个箱子的物体大于N/X,那么多的要搬出来。然后把搬出来的转移到前面X个里面小于N/X的地方。
当前箱子小于等于X,那么大的搬出来给小的。
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int num[];
char c[];
bool cmp(int a,int b){
return a>b;
}
int main()
{
int T,L,i,j,ans,tot;
scanf("%d",&T);
while(T--){
scanf("%s",c+);
L=strlen(c+); ans=L-;
for(i=;i<=;i++) num[i]=;
for(i=;i<=L;i++) num[c[i]-'a'+]++;
sort(num+,num++,cmp);
for(tot=;tot<=;tot++) if(num[tot]==) break; tot--;
for(i=;i<=;i++){
if(L%i==){
int tmp=;
if(tot>i){
for(j=;j<=i;j++) if(num[j]>L/i) tmp+=num[j]-L/i;
for(j=tot;j>i;j--) tmp+=num[j];
}
else{
for(j=;j<=tot;j++) if(num[j]>L/i) tmp+=num[j]-L/i;
}
if(tmp<ans) ans=tmp;
}
}
printf("%d\n",ans);
}
return ;
}
SPOJ:PATHETIC STRINGS(分配问题&贪心)的更多相关文章
- SPOJ:Elegant Permuted Sum(贪心)
Special Thanks: Jane Alam Jan*At moment in University of Texas at San Antonio - USA You will be give ...
- Codeforce-Ozon Tech Challenge 2020-B. Kuroni and Simple Strings(贪心)
B. Kuroni and Simple Strings time limit per test1 second memory limit per test256 megabytes inputsta ...
- SPOJ:The Next Palindrome(贪心&思维)
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- 【SPOJ】MGLAR10 - Growing Strings
Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is us ...
- Educational Codeforces Round 12 C. Simple Strings 贪心
C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...
- CF447B DZY Loves Strings 贪心
DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter ...
- SPOJ 7758. Growing Strings AC自动机DP
Growing Strings 题目:给出n个字符串,问最多能够选出多少个串组成序列,并满足前一个字符串是后一个字符串的子串. 分析: AC自动机经典水题... 考虑每个节点结尾时,他能够选出最多的串 ...
- 贪心:SPOJ Backup Files
BACKUP - Backup Files no tags You run an IT company that backs up computer data for large offices. ...
- 【贪心/Trie】【CF1083B】 The Fair Nut and Strings
Description 有 \(k\) 个长度为 \(n\) 的只含 \(a\) 或 \(b\) 字符串,并不知道它们具体是多少,只知道它们的字典序不小于字符串 \(A\),同时不大于字符串 \(B\ ...
随机推荐
- 什么是 Linux
什么是Linux Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络 ...
- codevs——2693 上学路线(施工)
2693 上学路线(施工) 时间限制: 2 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 问题描述 你所在的城市街道好像一个 ...
- OpenCV、PCL;Xtion、kinect;OpenNI、kinect for windows SDK比较
一.对比介绍: 1. OpenCV:开源跨平台,OpenCV于1999年由Intel建立,如今由Willow Garage提供支持. 2. OpenNI:OpenNI组织创建于2010年11月.主要成 ...
- CODEVS_1034 家园 网络流 最大流
原题链接:http://codevs.cn/problem/1034/ 题目描述 Description 由于人类对自然的疯狂破坏,人们意识到在大约2300年之后,地球不能再居住了,于是在月球上建立了 ...
- java消息队列怎么用
消息队列的使用场景是怎样的? 经常听到诸如rebbitmq,activemq,请教一下各位前辈消息队列的使用场景是怎样的,什么时候会用到它 校验用户名等信息,如果没问题会在数据库中添加一个用户记录 ...
- 用户空间&内核空间学习 & top命令 & time命令
参考了这篇文章 http://www.ruanyifeng.com/blog/2016/12/user_space_vs_kernel_space.html 简单说,Kernel space 是 Li ...
- [Tools] Region commands to collapse the code by group
For a file which contians lots of lines of code, we can use 'comments region' to collapse the code. ...
- 【转载】.NET Remoting学习笔记(二)激活方式
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科 ♂风车车.Net 激活方式概念 在访 ...
- MVC5中Model层开发数据注解 EF Code First Migrations数据库迁移 C# 常用对象的的修饰符 C# 静态构造函数 MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上) MVC 的HTTP请求
MVC5中Model层开发数据注解 ASP.NET MVC5中Model层开发,使用的数据注解有三个作用: 数据映射(把Model层的类用EntityFramework映射成对应的表) 数据验证( ...
- 深入理解JVM:HotSpot虚拟机对象探秘
对象的创建 java是一门面向对象的语言.在Java程序执行过程中无时无刻有Java对象被创建出来.在语言层面上,创建对象(克隆.反序列化)一般是一个newkeyword而已,而在虚拟机中,对象的创建 ...