Balala Power!【进制思维*经典】

Balala Power!

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


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  zinto 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
 

题意:给你n个由小写字母组成的字符串,让你给26个字母分配0-25,每个字符串形成一个26进制的数字,使这n个数的和尽可能大。(不能有前导0,但是单个0可以);

思路:利用26进制的思路去写,统计每一个字符处在每个位置的个数(满26注意进位),判断大小决定谁为0时,不能用sum和,因为取余了(我就那么天真的这么写了),按照其所处的位置大小进行排位,优先选出前导0的字符(贪心的味道),其余的一般处理,学习一下这个po数组的姿势;

//#pragma GCC optimize ("O2")
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define mod 1000000007
#define max_n 100010
typedef long long LL;
using namespace std;
bool d[1010];
char a[max_n];
LL po[max_n],num[30][max_n],sum[30];
LL n,ans,cnt,res,x;

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

int main()
{
	po[0]=1;
	for(int i=1;i<=max_n-2;i++)
		po[i]=26*po[i-1]%mod;
	while(scanf("%lld",&n)==1)
	{
		ans=0,cnt=-1,res=0,x=25;
		memset(sum,0,sizeof(sum));
		memset(num,0,sizeof(num));
		memset(d,false,sizeof(d));
		for(int i=0;i<n;i++)
		{
			scanf("%s",a);
			LL len=strlen(a);
			if(len>1) //bug点 
			{
				d[a[0]-'a']=true;
			}
			for(int j=0;j<len;j++)
			{
				num[a[j]-'a'][len-1-j]++;
				sum[a[j]-'a']+=po[len-j-1];
				sum[a[j]-'a']%=mod;
			}
			ans=max(ans,len);
		}
		for(int i=0;i<26;i++)
		{
			for(int j=0;j<ans;j++)
			{
				num[i][j+1]+=num[i][j]/26;
				num[i][j]%=26;
			}
			while(num[i][ans]) //bug点 
			{
				num[i][ans+1]+=num[i][ans]/26;
				num[i][ans++]%=26;
			}
			a[i]=i;
		}
		sort(a,a+26,cmp);
		for(int i=0;i<26;i++)
		{
			if(!d[a[i]])
			{
				cnt=a[i];
				break;
			}
		}
		for(int i=25;i>=0;i--)
		{
			if(a[i]!=cnt)
			{
				res+=(x--)*sum[a[i]]%mod;
				res%=mod;
			}
		}
		static int p=1; //新姿势 
		printf("Case #%d: %lld\n",p++,res);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值