题目:
Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2894 Accepted Submission(s): 637
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. (1≤n≤100000)
Each of the next n lines contains a string si consisting of only lower case letters. (1≤|si|≤100000,∑|si|≤106)
For each test case, the first line contains one positive integers n , the number of strings. (1≤n≤100000)
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
Recommend
题意是给了你n串字母,然后让你把这些字母映射成0-25之间的数,最后转换成26进制,使得和最大,要注意有前导0不合法,我们先找到那个赋值为0的字母,然后判断它有没有在首位出现过,如果出现过我们就循环左移,排除前导0的情况
思路就是先给每个字母根据它在每一位出现过的次数,确定权值,然后根据权值从大到小排序,然后依次赋值,最后排除前导0的情况,计算输出
代码:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const ll mod=1e9+7;
const ll N=1e5+7;
ll n;
string s[N];
ll num[30];//最后赋的值
ll mi[N];//26的次幂
ll vis[30];//标记某一个字母是否在第一位出现过
struct node
{
ll id;
ll num[N];//在某个位置出现的次数
bool operator < (const node &a) const
{
for(ll j=100000; j>=0; j--)
if(num[j]!=a.num[j])
return num[j]>a.num[j];
return 0;
}
} p[30];
int main()
{
ios::sync_with_stdio(false);
ll q=1;
mi[0]=1;
for(ll i=1; i<=N; i++)
mi[i]=(mi[i-1]*26)%mod;
while(cin>>n)
{
//初始化
for(ll i=0; i<26; i++)
{
for(ll j=0; j<=N; j++)
p[i].num[j]=0;
p[i].id=i;
vis[i]=0;
}
//读入n组数据,从右往左记录次数
for(ll i=0; i<n; i++)
{
cin>>s[i];
ll l=s[i].size();
if(l>1) vis[s[i][0]-'a']=1;
for(ll k=l-1,j=0; k>=0; k--,j++)
{
ll t=s[i][k]-'a';
p[t].num[j]++;
}
}
//进位
for(ll i=0; i<26; i++)
for(ll j=0; j<=N; j++)
{
p[i].num[j+1]+=p[i].num[j]/26;
p[i].num[j]%=26;
}
sort(p,p+26);
for(ll i=25; i>=0; i--)
num[p[25-i].id]=i;//排好序之后依次赋值
ll t=25;
while(vis[p[t].id]&&t)//判断有没有出现前导0,如果出现的话,就更改赋值的顺序
{
swap(num[p[t].id],num[p[t-1].id]);
t--;
}
ll ans=0;
for(ll i=0; i<n; i++)
{
ll l=s[i].size();
for(ll j=0; j<l; j++)
ans=(ans+num[s[i][j]-'a']*mi[l-1-j]%mod)%mod;
}
printf("Case #%lld: %lld\n",q++,ans);
}
return 0;
}