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(分配问题&贪心)的更多相关文章

  1. SPOJ:Elegant Permuted Sum(贪心)

    Special Thanks: Jane Alam Jan*At moment in University of Texas at San Antonio - USA You will be give ...

  2. 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 ...

  3. SPOJ:The Next Palindrome(贪心&思维)

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  4. 【SPOJ】MGLAR10 - Growing Strings

    Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is us ...

  5. Educational Codeforces Round 12 C. Simple Strings 贪心

    C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...

  6. CF447B DZY Loves Strings 贪心

    DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter ...

  7. SPOJ 7758. Growing Strings AC自动机DP

    Growing Strings 题目:给出n个字符串,问最多能够选出多少个串组成序列,并满足前一个字符串是后一个字符串的子串. 分析: AC自动机经典水题... 考虑每个节点结尾时,他能够选出最多的串 ...

  8. 贪心:SPOJ Backup Files

    BACKUP - Backup Files no tags  You run an IT company that backs up computer data for large offices. ...

  9. 【贪心/Trie】【CF1083B】 The Fair Nut and Strings

    Description 有 \(k\) 个长度为 \(n\) 的只含 \(a\) 或 \(b\) 字符串,并不知道它们具体是多少,只知道它们的字典序不小于字符串 \(A\),同时不大于字符串 \(B\ ...

随机推荐

  1. hanzi 全拼音 qu de

    Function pinyin(ByVal mystr As String, Optional types As Byte = 0) As StringDim temp   As String, i ...

  2. jquery.fullPage.js全屏滚动插件

    注:本文内容复制于http://www.51xuediannao.com/js/jquery/jquery.fullPage.html 和 http://www.360doc.com/content/ ...

  3. 发布npm包

    来源:https://segmentfault.com/a/1190000010398983

  4. API调用开发demo

    package fastjson; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStrea ...

  5. Angular2.X 笔记

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/7762590.html 前提: angular-cli (前 ...

  6. ios实现下载图片的裁减和显示

    使用如下的方法可以裁减的同时保证了不丢失像素. - (void)connectionDidFinishLoading:(NSURLConnection *)connection{    // Set ...

  7. 【Todo】Java8新特性学习

    参考这篇文章吧: http://blog.csdn.net/vchen_hao/article/details/53301073  还有一个系列

  8. [反汇编练习] 160个CrackMe之023

    [反汇编练习] 160个CrackMe之023. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  9. C语言枚举类型(Enum)

    在实际编程中,有些数据的取值往往是有限的,只能是非常少量的整数,并且最好为每个值都取一个名字,以方便在后续代码中使用,比如一个星期只有七天,一年只有十二个月,一个班每周有六门课程等. 以每周七天为例, ...

  10. NAND FLash基础概念介绍

    一.引脚介绍 引脚名称 引脚功能 CLE 命令锁存功能 ALE 地址锁存功能 /CE 芯片使能 /RE 读使能 /WE 写使能 /WP 写保护 R/B 就绪/忙输出信号 Vcc 电源 Vss 地 N. ...