You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.2 KiB
C++

#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
char s[N];
int cnt[26][N];
const long long mod = 1e9 + 7;
int main()
{
for (int n, t = 1; ~scanf("%d", &n); t++)
{
memset(cnt, 0, sizeof(cnt));
int maxlen = 0;
bool vis[26] = {false};
bool vis2[26] = {false};
for (int _ = 0; _ < n; _++)
{
scanf("%s", s);
vis2[*s - 'a'] = true;
int len = strlen(s);
maxlen = max(len, maxlen);
for (int i = 0; i < len; i++)
cnt[s[len - i - 1] - 'a'][i]++, vis[s[len - i - 1] - 'a'] = true;
}
set<char> S;
for (int i = 0; i < 26; i++)
if (vis[i] && !vis2[i])
S.insert(i + 'a');
maxlen += 10;
for (auto &i : cnt)
for (int j = 0, *arr = i; j < maxlen; j++)
arr[j + 1] += arr[j] / 26, arr[j] %= 26;
/*for (int i = 0; i < 26; i++)
{
printf("%c", 'a' + i);
for (int j = 0; j < maxlen; j++)
printf(" %d", cnt[i][j]);
putchar('\n');
}*/
pair<char, int *> res[26];
for (int i = 0; i < 26; i++)
res[i] = make_pair('a' + i, cnt[i]);
sort(res, res + 26, [maxlen](const pair<char, int *> &lhs, const pair<char, int *> &rhs) {
for (int i = maxlen; i >= 0; i--)
{
if (lhs.second[i] < rhs.second[i]) return true;
if (lhs.second[i] > rhs.second[i]) return false;
}
return false;
});
if (vis2[res[0].first - 'a'])
for (auto &re : res)
if (S.count(re.first))
{
swap(res[0], re);
break;
}
/*for (auto &re : res)
{
printf("%c ", re.first);
for (int j = 0; j < maxlen; j++)
printf("%d ", re.second[j]);
putchar('\n');
}*/
long long ans = 0, base = 1;
for (int i = 0; i < maxlen; i++, base = base * 26 % mod)
for (int j = 0; j < 26; j++, ans %= mod)
ans += j * base * res[j].second[i];
printf("Case #%d: %lld\n", t, ans);
}
return 0;
}