HDU 2017 多校联合训练赛1 1002 6034 Balala Power 排序

Balala Power!

Time Limit: 4000/2000 MS (Java/Others)  

 Memory Limit: 131072/131072 K (Java/Others)



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
2017 Multi-University Training Contest - Team 1


题目大意
给出n个字符串,字符串中只包含26个小写字母,现在给每个字母赋权值0~25,并且把每个字符串当作26进制数看待,问,如何对字母赋值,使n个字符串代表的26进制数之和最大,并输出这个和mod 10e9+7。


题目分析
题目要求字符串代表的26进制数之和最大,就要将高权值尽可能地赋给26进制数中在高位出现频率更高的字母,这样,问题就分成了三部分。第一,字母按价值排序;第二,字母赋值;第三,求和。

第一步,排序问题,为了方便比较,我们建立一个26*100000的数表,记录每个字母在每一位上的出现频数,从高位开始比较出现频数最高的字母赋值为25。在这里要注意到,因为有些字母可能会在某一位出现若干多次(超过26),这样该字母在本位上的价值可能会超过更高位上的字母价值,因此,我们运用数制进位的思想,将出现频数超过26的字母向上进位,这样就保证了在高位字母价值的绝对优势。在比赛时手动进行了字母排序,不仅耗费了大量的精力,而且最终。。。超时了。。。赛后看标程,才深深地体会到cmp函数的神奇。

第二步,排序完成后,字母们整齐的码在一维数组里。按价值由高到低赋值即可。但是,还要注意前导0的问题,出现在字符串首的字母是不能被赋值为0的,在这里我们将价值最低且没有出现在字符串首的字母标记出来。然后将剩余的字母按价值赋值。

第三步,求和。即,26进制转10进制。我的方法和标程略有差别,标程引入了sum[ ]数组,但是时间复杂度貌似差别不大,

另外,还有一些小细节。
1.计算26^n,用打表代替快速幂取模,可以大大降低时间复杂度
2.由于进位的问题,在字符串比较 和 进制转化时,注意要比最长字符串的长度ml再多比较一位。


上代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod = 1e9+7;
int n;
int l, ml;
int G[30][100005];
int ban[30];
char s[100005];
int sum[100005];
int power[100005];
int val[30];

bool cmp(int A, int B)
{
    for (int i=ml; i>=0; --i)
        if (G[A][i] != G[B][i])
            return G[A][i] < G[B][i];
    return 0;
}

void init()
{
    power[0] = 1;
    for (int i=1; i<100005; ++i)
    {
        power[i] = (long long)power[i-1]*26%mod;
    }
}

int main()
{
    init();
    while (~scanf ("%d",&n))
    {
        memset(G , 0 , sizeof(G));
        memset(sum , 0 , sizeof(sum));
        memset(ban , 0 , sizeof(ban));
        for (int i=0; i<26; ++i)
            val[i] = i;
        ml = 0;
        for (int i=0; i<n; ++i)
        {
            scanf ("%s",s);
            l = strlen(s);
            if (l > ml)
                ml = l;
            if (l > 1)
                ban[s[0]-'a'] = 1;
            reverse(s, s+l);
            for (int j=0; j<l; ++j)
            {
                G[s[j]-'a'][j] ++;
                if (G[s[j]-'a'][j] >= 26)
                {
                    G[s[j]-'a'][j+1] += G[s[j]-'a'][j]/26;
                    G[s[j]-'a'][j] %= 26;
                }
                sum[s[j] - 'a'] += power[j];
                if (sum[s[j] - 'a'] >= mod)
                {
                    sum[s[j] - 'a'] -= mod;
                }
            }
        }

        /*for (int i=0; i<=40; i++)
        {
            for (int j=0; j<=26; j++)
                printf ("%d ",G[j][i]);
            printf ("\n");
        }*/

        sort(val, val+26, cmp);

        for (int i=0; i<26; ++i)
            printf("%d ",val[i]);
        printf ("\n");

        /*for (int i=0; i<26; ++i)
            printf("%d ",sum[i]);
        printf ("===sum\n");*/

        int zero;
        for (int i=0; i<26; ++i)
        {
            if (ban[val[i]] == 0)
            {
                zero = val[i];//未出现在字符串开头,且价值最小
                break;
            }
        }
        int ans = 0, x = 25;//x:字母权值
        for (int i=25; i>=0; --i)
        {
            if (val[i] != zero)
            {
                ans = (ans+(long long)(x--)*sum[val[i]]%mod)%mod;
                //printf ("sum-%d  x-%d  val-%d\n",sum[val[i]],x,val[i]);
            }

        }
        static int cnt = 0;
        printf ("Case #%d: %d\n",++cnt, ans);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod = 1e9+7;
int n;
int l, ml;
int G[30][100005];
int vis[30];
int ban[30];
char s[100005];
int power[100005];
int val[30];

bool cmp(int A, int B)
{
    for (int i=ml; i>0; --i)
        if (G[A][i] != G[B][i])
            return G[A][i] < G[B][i];
    return 0;
}

void init()
{
    power[0] = 1;
    for (int i=1; i<100005; ++i)
    {
        power[i] = (long long)power[i-1]*26%mod;
    }
}

int main()
{
    //freopen("1002.in", "r", stdin);
    //freopen("1002.txt", "w", stdout);
    init();
    while (~scanf ("%d",&n))
    {
        memset(G , 0 , sizeof(G));
        memset(vis , 0 , sizeof(vis));
        memset(ban , 0 , sizeof(ban));
        for (int i=0; i<26; ++i)
            val[i] = i;
        ml = 0;
        for (int i=0; i<n; ++i)
        {
            scanf ("%s",s);
            l = strlen(s);
            if (l > ml)
                ml = l;
            if (l > 1)
                ban[s[0]-'a'] = 1;
            reverse(s, s+l);
            for (int j=0; j<l; ++j)
            {
                vis[s[j]-'a'] = 1;
                G[s[j]-'a'][j] ++;
                if (G[s[j]-'a'][j] >= 26)
                {
                    G[s[j]-'a'][j+1] += G[s[j]-'a'][j]/26;
                    G[s[j]-'a'][j] %= 26;
                }
            }
        }

        /*for (int i=0; i<=40; i++)
        {
            for (int j=0; j<=26; j++)
                printf ("%d ",G[j][i]);
            printf ("\n");
        }*/

        sort(val, val+26, cmp);

        /*for (int i=0; i<26; ++i)
            printf("%d ",val[i]);
        printf ("\n");*/

        int zero;
        for (int i=0; i<26; ++i)
        {
            if (ban[val[i]] == 0)
            {
                zero = val[i];//未出现在字符串开头,且价值最小
                break;
            }
        }
        int ans = 0, x = 25;//x:字母权值
        for (int i=25; i>=0; --i)
        {
            if (val[i] != zero && vis[val[i]])
            {
                for (int j=0; j<=ml; ++j)
                {
                    ans = (ans + (long long)G[val[i]][j]*x*power[j]%mod)%mod;
                    //printf ("G-%d  x-%d  p-%d \n",G[val[i]][j],x,power[j-1]);
                }
                x--;
            }
        }
        static int cnt = 0;
        printf ("Case #%d: %d\n",++cnt, ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值