牛客寒假算法基础集训营3 - J 处女座的比赛 字符串hash

题目链接

题意:根据所给的 h a s h hash hash 函数,对于题目所输入的字符串,输出与输入字符串不同但是 h a s h hash hash 值相同的字符串。

思路:长度为 1 1 1 4 4 4 的字符串一共有 475254 475254 475254 种,足以把 [ 0 , 9982 ] [0,9982] [0,9982] 中所有数都覆盖至少 2 2 2 次。所以可以预处理这些字符串,每个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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值