题意:有n个字符串,为出现过的每种字母赋一个0~25的值,要求每种字母的值各不相同。要求,若一个字母是一个长度大于1的字符串的首字母,则该字母不能为0。为所有字母赋值后,所得到的每个字符串都是二十六进制数,问这些二十六进制数转化为十进制后的数值之和。
分析:
1、统计出现过的每种字母在每一位上的出现次数。若某一位的出现次数>=26,则进位。
2、按照处理过的各字母在每一位上的出现次数排序,高位出现次数多的优先级高,并依次为每个字母赋值。
3、若优先级最低的字母j不能赋值为0,则从优先级由低到高,找到第一个可以赋值为0的字母i将其赋值为0,并将i之后的字母的赋值数+1。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const LL MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 1000000 + 10;
using namespace std;
char s[MAXN];
int cnt[30][MAXN];
int maxlen;
set<int> st;
struct Node{
int c;
bool leadingzero;
int value;
bool operator < (const Node& rhs)const{
for(int i = maxlen - 1; i >= 0; --i){
if(cnt[c][i] != cnt[rhs.c][i]) return cnt[c][i] > cnt[rhs.c][i];
}
return c < rhs.c;
}
}num[30];
int main(){
int n;
int kase = 0;
while(scanf("%d", &n) == 1){
memset(cnt, 0, sizeof cnt);
st.clear();
maxlen = 0;
for(int i = 0; i < 26; ++i) num[i].c = i, num[i].leadingzero = false;
for(int i = 0; i < n; ++i){
scanf("%s", s);
int len = strlen(s);
if(len > 1) num[s[0] - 'a'].leadingzero = true;
maxlen = max(maxlen, len);
for(int j = 0; j < len; ++j){
++cnt[s[j] - 'a'][len - 1 - j];
st.insert(s[j] - 'a');
}
}
for(int i = 0; i < 26; ++i){
for(int j = 0; j < maxlen - 1; ++j){
while(cnt[i][j] >= 26){
cnt[i][j] -= 26;
++cnt[i][j + 1];
}
}
}
sort(num, num + 26);
for(int i = 0; i < st.size(); ++i){
num[i].value = 25 - i;
}
if(st.size() == 26){
int id = 0;
for(int i = 25; i >= 0; --i){
if(num[i].leadingzero == false){
id = i;
break;
}
}
num[id].value = 0;
for(int i = id + 1; i <= 25; ++i){
++num[i].value;
}
}
LL ans = 0;
for(int i = 0; i < 26; ++i){
LL tmp = 0;
LL x = 1;
for(int j = 0; j < maxlen; ++j){
(tmp += (x * (LL)cnt[num[i].c][j]) % MOD) %= MOD;
(x *= (LL)26) %= MOD;
}
(ans += (tmp * (LL)num[i].value) % MOD) %= MOD;
}
printf("Case #%d: %lld\n", ++kase, ans);
}
return 0;
}