2017 Multi-University Training Contest - Team 1 B - Balala Power! (思维代码能力,贪心)

64 篇文章 0 订阅
25 篇文章 4 订阅

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  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 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 xindicates 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
题目大意:每次会给n个字符串,你可以对于每个字母进行赋值,0~25,不允许字符串的首位为0,要求算出26进制的最大和
解题思路:对于每个字母所出现的位数进行统计,然后对26个字母进行排序,优先让那些出现在高位,出现次数多的尽可能赋予大数,同时判一下前导0
这道题在进行排序和整合的时候超级麻烦,都是基本上思路上对了,但是在代码实现上直接GG
#include<iostream>  
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<climits>   
#include<cmath> 
#include<vector>  
#include <bitset>  
#include<algorithm>    
#include <queue>  
#include<map> 
#define inf 9999999;
using namespace std;
typedef long long LL;

int b[30],L;
bool check[30];
LL a[30][100010];
LL pt=1e9+7;
LL pows[100010];
LL sum[100010],ans;
bool cmp(int x,int y)
{
	for(int i=L-1;i>=0;i--)
	{
		if(a[x][i]!=a[y][i])
			return a[x][i]<a[y][i];
	}
	return 0;
}

int main()
{
	int i,j,len,n;
	int k=0;
	string str;
	pows[0]=1;
	for(i=1;i<100005;i++)
	{
		pows[i]=pows[i-1]*26%pt;
	}
	while(cin>>n)
	{
	//	int L;
		L=0;
		memset(sum,0,sizeof(sum));
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(check,0,sizeof(check));
		for(i=1;i<=n;i++)
		{
			cin>>str;
			len=str.length();
			for(j=0;j<len;j++)
			{
				if(len>1)
					check[str[0]-'a']=1;
				a[str[j]-'a'][len-j-1]++;
				//cout<<pows[len-j-1]<<endl;
				sum[str[j]-'a']=(sum[str[j]-'a']+pows[len-j-1])%pt;
			}
			L=max(L,len);
		}
		for(i=0;i<26;i++)
		{
			for(j=0;j<L;j++)
			{
				a[i][j+1]+=a[i][j]/26;
				a[i][j]%=26;
			}
			while(a[i][L])//这是个坑考虑到最高位置满26后要上进一位
			{
				a[i][L+1]+=a[i][L]/26;
				a[i][L++]%=26;//这里L++的原因是这个位置由于原先L的位置满26进位,所以要考虑进去,同理在sort中也是
			}
			b[i]=i;
		}
		sort(b,b+26,cmp);
		int cnt=-1;
		for(i=0;i<26;i++)
		{
			if(check[b[i]]==0)
			{
				cnt=b[i];
				break;
			}
		}
		ans=0;
		int x=25;
		for(i=25;i>=0;i--)
		{
			if(b[i]!=cnt)
			{
				ans=(ans+(x--)*sum[b[i]])%pt;
			}
		}
		cout<<"Case #"<<++k<<": "<<ans<<endl;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值