HDU 6034 Balala Power!(贪心)

Balala Power!

Problem Description

Talented  Mr.Tang has  n  strings consisting of only lower case characters. He wants to charge them with Balala Power (he could change each character ranged from  a to  z into each number ranged from  0 to  25, but each two different characters should not be changed into the same number) so that he could calculate the sum of these strings as integers in base  26  hilariously.

Mr.Tang wants you to maximize the summation. Notice that no string in this problem could have leading zeros except for string "0". It is guaranteed that at least one character does not appear at the beginning of any string.

The summation may be quite large, so you should output it in modulo  109+7 .
Input
The input contains multiple test cases.

For each test case, the first line contains one positive integers  n , the number of strings.  (1n100000)

Each of the next  n  lines contains a string  si  consisting of only lower case letters.  (1|si|100000,|si|106)
 

Output
For each test case, output " Case # x y " in one line (without quotes), where  x  indicates the case number starting from  1  and  y  denotes the answer of corresponding case.
 

Sample Input
   
   
1 a 2 aa bb 3 a ba abc
 

Sample Output
   
   
Case #1: 25 Case #2: 1323 Case #3: 18221
 

Source


题解:将每个字母的个数用26进制统计,从大到小排序(字符串排序),贪心分配数值
如果出现前导0的情况,找出最小的可以赋为0的字母,将后面的字母往前递推

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<queue>
#include<stack>
#include<math.h>
#define N 100005
#define pi 3.1415926
#define FLAG 9999999
#define mod 1000000007
#define min(a,b) (a<b ? a:b)
using namespace std;
typedef long long ll;

char str[N];
ll map[30][N];
ll res[30];
ll l[N];
ll mm;
struct ww{
    ll data;
    ll cur;
    char a[N];


}tmp[26];
bool cmp(ww x,ww y)
{
    if(strcmp(y.a,x.a)>0)
        return 1;
    else return 0;
}

int main()
{

    ll i,j,k;
    ll n;
    ll y=0;
    while(cin>>n)
    {
        y++;
        k = 0;
        memset(map,0,sizeof(map));
        memset(l,0,sizeof(l));
        mm=0;
        for(i = 0; i < n; i++)//统计字母个数
        {
            cin>>str;
            ll len = strlen(str);
            if(len>1) l[str[0]-'a']=1;;
            k = max(k,len);

            ll w=0;
            for(j = len-1; j >= 0; j--)
            {
                map[str[j]-'a'][w] += 1;
                w++;

            }
        }
        for(i=0;i<100005;i++)//进位
        {
            int flag=0;
            for(j=0;j<26;j++)
            {
                if(map[j][i]!=0) flag=1;
                if(map[j][i]>=26)
                {
                    map[j][i+1]+=map[j][i]/26;
                    map[j][i]%=26;
                }
            }
            if(flag==1)
                mm=max(mm,i);//记录最高位
        }

        memset(res,0,sizeof(res));

        for(int j=0;j<26;j++)//转换为字符串
        {
            memset(tmp[j].a,0,sizeof(tmp[j].a));
            tmp[j].cur=j;
            ll w=0;
            for(ll i=mm;i>=0;i--)
               {
                   tmp[j].a[w]=map[j][i]+'0';
                   w++;
               }
        }
        sort(tmp,tmp+26,cmp);//排序
        ll x=0;
        for(j = 0; j < 26; j++)//从小到大分配数值
        {
            res[tmp[j].cur] = x;
            x++;
        }
        ll f[26];
        for(i=0;i<26;i++)
            f[res[i]]=i;
        for(i=0;i<26;i++)//处理前导0的情况
        {
            if(l[i]&&res[i]==0)
            {
                ll ss=26;
                ll flag=0;
                for(j=0;j<26;j++)
                {
                    if(l[j]) continue;
                    if(res[j]<ss)
                    {
                        ss=res[j];
                        flag=j;
                    }
                }
                for(j=1;j<ss;j++)
                {
                    res[f[j]]++;
                }
                res[i]=1;
                res[flag]=0;
            }
        }


        ll sum = 0;
        ll t=1;
        for(j =0; j <=mm; j++)//计算结果
        {
            for(i = 0; i < 26; i++)
            {
                if(map[i][j]==0) continue;
                if(res[i]==0) continue;
                //cout<<res[i]<<endl;
                ll tm=t;
                //cout<<tm<<" ";
                tm = (tm * res[i]) % mod;
                tm=(tm * map[i][j])%mod;
                //cout<<i<<" "<<res[i]<<endl;
                sum = (sum+tm) % mod;
            }
            t=(t*26)%mod;
        }
        cout<<"Case #"<<y<<": ";
        printf("%lld\n",sum);
       }
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值