hdu6034(Balala Power!)

Balala Power!

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4247    Accepted Submission(s): 1055


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
 

题目大意:求用多个a,b,c,...,z表示的26进制数字的总和,其中a,b,c,...,z与0~25的对应关系不确定,要求不能有前缀0,求出最大的总和。
解题思路:先算出每一位上a,b,c,...,z的个数,然后分别组成26个26进制数,通过基数排序从低到高比较每个字母的数量(包括进位),排出a,b,c,...,z的权值大小,为避免前缀0,在讲权值最小的非首字母的字母权值定为0,其他顺序不变,然后根据每一位字母的个数和字母的权值,求出最后的总和。
代码如下:

#include <bits/stdc++.h>
using namespace std;
#define inf 1000000007
#define LL long long
int a[110000][26];
struct aaa
{
   LL sum;
   bool flag; 
   int id;
}b[26];
bool operator <(aaa a,aaa b)
{
	return a.sum<b.sum;
}
LL qmod(LL a,LL b,LL n)    //快速幂去摸 
{     
    LL ret=1;     
    LL tmp=a;     
    while(b)     
    {   
       if(b&0x1) ret=ret*tmp%n;     
       tmp=tmp*tmp%n;     
       b>>=1;     
    }     
    return ret;     
} 
void sortt()//稳定的排序
{
	int i,j;
	for(i=1;i<26;i++)
	{
		for(j=i;j>0;j--)
		{
			if(b[j]<b[j-1])
				swap(b[j],b[j-1]);
			else
				break;
		}
	}
}
int main()
{
	std::ios::sync_with_stdio(false);
	string s;
	int n,t=0,i,j,l,ma,tmp[26];//tmp保存进位
	LL ans;
	while(cin>>n)
	{
		t++;
		memset(a,0,sizeof(a));
		for(i=0;i<26;i++)
		{
			b[i].flag=0;
			b[i].sum=0;
			b[i].id=i;
		}
		ma=0;
		for(i=0;i<n;i++)
		{
			cin>>s;
			l=s.length();
			b[s[0]-'a'].flag=1;
			ma=max(ma,l);
			for(j=0;j<l;j++)
			{
				a[l-1-j][s[j]-'a']++;
			}
		}
		memset(tmp,0,sizeof(tmp));
		for(i=0;i<=ma+100;i++)
		{
			for(j=0;j<26;j++)
			{
				b[j].sum=(tmp[b[j].id]+a[i][b[j].id])%26;
				tmp[b[j].id]=(tmp[b[j].id]+a[i][b[j].id])/26;
			}
			sortt();
		}
		//避免首0
		for(i=0;i<26;i++)
		{
			if(b[i].flag==0)
			{
				break;
			}
		}
		for(;i>0;i--)
		{
			swap(b[i],b[i-1]);
		}
		ans=0;
		for(i=25;i>=0;i--)
		{
			for(j=0;j<ma;j++)
			{
				ans+=qmod(26,j,inf)*a[j][b[i].id]*i;
				ans=ans%inf;
			}
		}
		cout<<"Case #"<<t<<": "<<ans<<endl;
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值