多校 ——Balala Power!


Balala Power!



Talented Mr.Tang has n 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 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 109+7.

Input
The input contains multiple test cases.

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

Each of the next
n n lines contains a string si si consisting of only lower case letters. (1|si|100000,|si|106) (1≤|si|≤100000,∑|si|≤106)
Output
For each test case, output " Case # x x: y y" in one line (without quotes), where x x indicates the case number starting from 1 1 and y 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


官方题解:每个字符对答案的贡献都可以看作一个 26 进制的数字,问题相当于要给这些贡献加一个 0 到 25 的权重使得答案最大。最大的数匹配 25,次大的数匹配 24,依次类推。排序后这样依次贪心即可,唯一注意的是不能出现前导 0。
反正我是看别人代码然后敲了好长时间,自己写的时候各种问题已然无解。
结构体里数组存每个字母所在位置的个数,然后26进制当然是进位了,
sort的时候注意顺序。
然后就是前导零的问题,当字符串只有一个字符的时候可以出现前导零,否则就必须标记这个字符,
最后在遍历的时候找到可以赋值为零的字符,并给这个字符前面和后面的值进行更改

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 100000+100
using namespace std;

const int MOD=1000000007;
typedef long long ll;
struct Node{

 int a[maxn];
 int pos;

}A[30];
bool vis[30];
char s[maxn];
bool cmp(Node &m,Node &n)
{
    for(int i=maxn-1;i>0;i--)
    {
        if(m.a[i]!=n.a[i])
            return m.a[i]<n.a[i];

    }
    return m.a[0]<n.a[0];
}
int main()
{
    int kase=1;
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(vis,false,sizeof vis);
        for(int i=0;i<26;i++)
        {
            A[i].pos=i;
            for(int j=0;j<maxn;j++)
                A[i].a[j]=0;
        }
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            int len=strlen(s)-1;
            if(len>0)
                vis[s[0]-'a']=true;
            for(int j=0;j<=len;j++)
            {
                int x=s[j]-'a';
                int y=len-j;
                A[x].a[y]++;
                while(A[x].a[y]==26)
                {
                    A[x].a[y++]=0;
                    A[x].a[y]++;
                }
            }

        }
        sort(A,A+26,cmp);
        int temp=-1;
        for(int i=0;i<26;i++)
        {
            if(!vis[A[i].pos])
            {
                temp=i;
                break;
            }
        }
        ll tab,ans=0,mul;
        for(int i=0;i<26;i++)
        {
            tab=0;
            mul=0;
            if(i==temp)
                mul=0;
            else
                if(i<temp)
                    mul=i+1;
            else
                mul=i;
            for(int j=maxn-1;j>=0;j--)
            {
                tab=(tab*26)%MOD;
                tab=(tab+(ll)A[i].a[j]*mul)%MOD;

            }
            ans=(ans+tab)%MOD;
        }
      printf("Case #%d: %lld\n",kase++,ans);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值