题解:hash+map。
比赛的时候没用hash,直接用map存字符串,竟然过了。。。
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
string s[100005];
map<ull, int> mp;
int d[30];
void hash_(char s[])
{
int slen = strlen(s);
ull ans = 1, base = 131;
for (int i = 0; i < slen; i++)
{
ans = (ull)s[i] + 1 + ans * base;//自动取模
mp[ans]++;
}
return;
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < 26; i++)
scanf("%d", &d[i]);
for (int i = 1; i <= n; i++)
{
char a[100005];
cin >> s[i];
for (int j = 0; j < s[i].length(); j++)
{
a[j] = s[i][j];
}
a[s[i].length()] = 0;
hash_(a);
}
for (int i = 1; i <= n; i++)
{
ull temp_ans = 1, temp = 1, ans = 0;
for (int j = 0; j < s[i].length(); j++)
{
temp_ans = (d[s[i][j] - 'a'] * temp_ans) % m;
}
ull ans__ = 1, base = 131;
for (int j = 0; j < s[i].length(); j++)
{
temp = (d[s[i][j] - 'a'] * temp) % m;
ans__ = (ull)s[i][j] + 1 + ans__ * base;//自动取模
if (temp > temp_ans)
{
ans += mp[ans__];
}
}
cout << ans << ' ';
}
return 0;
}