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[27];
char c[2010];
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+1);
        L=strlen(c+1); ans=L-1;
        for(i=1;i<=26;i++) num[i]=0;
        for(i=1;i<=L;i++) num[c[i]-‘a‘+1]++;
        sort(num+1,num+26+1,cmp);
        for(tot=1;tot<=26;tot++) if(num[tot]==0)  break; tot--;
        for(i=1;i<=26;i++){
            if(L%i==0){
                int tmp=0;
                if(tot>i){
                    for(j=1;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=1;j<=tot;j++)  if(num[j]>L/i) tmp+=num[j]-L/i;
                }
                if(tmp<ans) ans=tmp;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
 

原文地址:https://www.cnblogs.com/hua-dong/p/8971006.html

时间: 2024-07-29 05:51:31

SPOJ:PATHETIC STRINGS(分配问题&贪心)的相关文章

SPOJ MSTICK. Wooden Sticks 贪心 结构体排序

MSTICK - Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine

Two strings(贪心)

题解:一道在别人口中很水的贪心题,然而在考试的时候就没有想到,只拿了50分.下文中a为第一个字符串,b为第二个字符串. 我们维护两个数组,一个是l[i],一个是r[i].l[i]表示b[0...i]在a中按题意完全匹配(从前向后匹配)的最前的位置,r[i]表示b[i...lenb]按题意从后向前匹配的最后的位置.这两个数组其实具有单调性,可以O(len)求出.接着我们枚举i,表示b[0...i]留下来,假如有个j,满足l[i]<r[j],那么把b[i+1...j-1]移走是满足题目要求的.而且随

UESTC 2014 Summer Training #16 Div.2

虽然被刷了还是要继续战斗下去嗯...就是基础不好,难度相对较大 A.SPOJ AMR10A 点是顺时针给出的,可以在图上画画(脑补也行),连线x-a,x-b(x为选定的一个点,比如第一个点),就把所求面积分成了四部分,a-b左边部分是较容易求出来的, 三角形面积是直接可求,另外两个多边形面积是可以预处理出来的(多个三角形面积和) 反正我是沒想出來...看題解也理解半天,多邊形面積转化为三角形面积和 嗯嗯 #include <iostream> #include <cstdio> #

贪心算法之活动分配问题

贪心算法之活动分配问题 在此之前,我们还讨论过贪心算法的活动选择问题,活动选择问题里面的选择策略在这篇文章里面作为贪心选择策略用到.好吧,让我们进入主题. 问题描述 有一个活动集合S={a1,a2,a3,...an},每一个活动ai都有一个开始时间si和结束时间fi,那么活动ai占用的时间段为[si,fi).如果活动ai和aj的时间段没有交集重叠,那么这两个活动是兼容的,即满足si≤fj或者fi≥sj,[ai,aj]就是兼容的.现在我们需要为这些活动安排教室,保证活动之间各不冲突.请问怎么安排才

【SPOJ】MGLAR10 - Growing Strings

Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters. Strings have the particularity that, as they grow, they add

SPOJ 7758 Growing Strings

MGLAR10 - Growing Strings Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters. Strings have the particularity tha

SPOJ - LOCKER 数论 贪心

题意:求出\(n\)拆分成若干个数使其连乘最大的值 本题是之江学院网络赛的原题,计算规模大一点,看到EMAXX推荐就做了 忘了大一那会是怎么用均值不等式推出结果的(还给老师系列) 结论倒还记得:贪心分解3,不够就用2凑 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #

SPOJ:Decreasing Number of Visible Box(不错的,背包?贪心?)

Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman wants to hide boxes as many as possible. A box can be kept hidden inside of another box if and only if the box in which it will be held is empty and

SPOJ 417 The lazy programmer(贪心)

417. The lazy programmer Problem code: LAZYPROG A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is a web-designer and an executive director at the same time. The second one is a programmer. The directo