题意:根据所给的 函数,对于题目所输入的字符串,输出与输入字符串不同但是 值相同的字符串。
思路:长度为 到 的字符串一共有 种,足以把 中所有数都覆盖至少 次。所以可以预处理这些字符串,每个hash值存下对应两种不同的字符串即可。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int mod=9983;
int p, q, r, t;
int T, in_dex[26], mul[3];
string s;
int cnt[9983] = {};
string ans[9983][2];
inline int get_hash()
{
int ans = 0, len = s.size();
for (int i=1;i<=len;i++)
ans = (ans * mul[i % 3] + in_dex[s[i - 1] - 'a']) % mod;
return ans;
}
inline void add(char x)
{
s += x;
int hash = get_hash();
if (cnt[hash] < 2)
{
ans[hash][cnt[hash]] = s;
++cnt[hash];
}
}
int main()
{
scanf("%d%d%d%d%d%d", &p, &q, &r, &t, &T);
mul[0] = p, mul[1] = q, mul[2] = r;
for (int i = 0; i < 26; i++)
in_dex[i] = i * t + t;
for (char i = 'a'; i <= 'z'; ++i)
{
s = "", add(i);
for (char j = 'a'; j <= 'z'; ++j)
{
add(j);
for (char k = 'a'; k <= 'z'; ++k)
{
add(k);
for (char z = 'a'; z <= 'z'; ++z)
add(z), s.pop_back();
s.pop_back();
}
s.pop_back();
}
s.pop_back();
}
int hash;
while (T--)
{
cin >> s;
hash = get_hash();
if(s == ans[hash][0])
cout << ans[hash][1];
else
cout << ans[hash][0];
putchar('\n');
}
return 0;
}