Not BB,show you the code!
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const long long M = 1e9+7;
const int MAX = 1e5+5;
struct Node
{
char key[MAX];
int c;
}node[30];
int vis[30];
long long val[MAX];
char s[MAX];
void init()
{
memset(vis,0,sizeof(vis));
for (int i = 0 ; i < 26; i++)
{
node[i].c = i;
for (int j = 0; j < MAX; j++)
{
node[i].key[j] = 0;
}
}
}
bool cmp(Node &a,Node &b)
{
for (int i = MAX-1; i>=0; i--)
{
if (a.key[i]!=b.key[i])
return a.key[i] > b.key[i];
}
return a.key[0] > b.key[0];
}
LL cal(int p,int k)
{
LL sum = 0;
for(int i = 0 ; i < MAX; i++)
{
sum = (sum + val[i]*node[p].key[i])%M;
}
return (sum*k)%M;
}
int main()
{
int cas = 0;
int n;
val[0] = 1;
for (int i = 1; i < MAX; i++)
val[i] = (val[i-1]*26)%M;
while(cin >> n)
{
init();
for (int i = 0 ; i < n; i++)
{
scanf("%s",s);
int len = strlen(s);
if (len>1)
vis[s[0]-'a'] = 1;
for (int j = len-1; j >= 0; j--)
{
int pos = len-j-1;
int num = s[j]-'a';
node[num].key[pos]++;
while(node[num].key[pos]>25)
{
node[num].key[pos] = 0;
pos++;
node[num].key[pos]++;
}
}
}
sort(node,node+26,cmp);
int p = 25;
while(vis[node[p].c]) p--;
int k = 25;
LL ans = 0;
for (int i = 0 ; i < 26; i++)
{
if (i==p)
continue;
ans = (ans+ cal(i,k))%M;
k--;
}
printf("Case #%d: %lld\n",++cas,ans);
}
}