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.
输入
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)
输出
样例输入
1
a
2
aa
bb
3
a
ba
abc
样例输出
Case #1: 25
Case #2: 1323
Case #3: 18221
题目大意就是给出n个26进制数,但是用字母表示每一位,要求n数之和最大输出这个和
做法:如果是十进制的数,我们都会想,将每个字母的个数记录下来,然后进行求值,如 ab 则a有10个,b有一个,则a选9,b选8 ab最大的情况就是 98 那么换成26进制同样如此,只不过要注意用数组来记录个数,因为给出的数字太大,long long int 也无法保存字母的个数
#include<iostream>//这个代码有误,思路没问题但提示越界
#include<string>
#include<cstring>
#include<stdio.h>
#include <algorithm>
#include<math.h>
using namespace std;
struct qz
{
int t;
int ms[700]={0};//存26进制的个数
long long int st=0;//直接得到10进制的个数(对1000000007取余)
};
long long int mod(qz a,qz b)//进行出现次数的排列
{
return a.st<b.st;
}
int main()
{
int k=1;
long long int f[180];
f[0]=1;
for(int i=1;i<120;i++)//计算26进制转10进制的每一位
{
f[i]=f[i-1]*26%1000000007;
}
int n;
while(cin>>n)
{
int b[40]={0};
int s=0;
qz a[40];
for(int i=0;i<26;i++)
a[i].t=i;
while(n--)
{
string s;
cin>>s;
int j=0;
if(s.length()>0)//首位不为0判断
b[s[0]-'a']=1;
for(int i=s.length()-1;i>=0;i--)//计算每个字母的数目
{
a[s[i]-'a'].ms[j]++;
int h=j;
j++;
while(a[s[i]-'a'].ms[h]==26)
{
a[s[i]-'a'].ms[h]=0;
h++;
a[s[i]-'a'].ms[h]++;
}
}
}
for(int i=0;i<26;i++)
{
int j=120;
while(a[i].ms[j]==0)
{
j--;
if(j<0)
{
s++;
break;
}
}
for(int m=0;m<=j;m++)//直接转换成10进制
{
a[i].st+=a[i].ms[m]*f[m];
a[i].st=a[i].st%1000000007;
}
}
sort(a,a+26,mod);
long long int u=0;
if(s==0)//首位不为0判断
{
int y=1;
for(int i=0;i<=25;i++)
{
if(s==0&&b[a[i].t]==0)
{
u+=0;
s=1;
}
else
{
u+=y*a[i].st;
u=u%1000000007;
y++;
}
}
}
else
{
for(int i=s;i<=25;i++)
{
u+=i*a[i].st;
u=u%1000000007;
}
}
printf("Case #%d: %lld\n",k++,u);
}
return 0;
}