FZU1860 :Funny Hash game I(位运算)


FZU1860 :Funny Hash game I

时间限制:1000MS    内存限制:32768KByte   64位IO格式:%I64d & %I64u
描述
A classic string hashing algorithm can be simply described by the following code:

unsigned long long hash(string str) { unsigned long long ret= 5381; int i; for (i=0;i < str.size();i++) ret=(ret* 33)^str[i]; //xor operation return ret; }

You will be given a hash value,and you shall determine that whether there exists a string with this hash value.

Note that the string you searching for must satisfy the following two conditions:

1.The length of the string is not large than 8.

2.The string is composed of characters whose ASCII code are between [0,128) .

输入
Input contains multiple cases.Test cases are separated by several blank lines.

Each test case starts with a integer N(0 <= N < 2^64),which reperesents the hash value.

There are no more than 1000 test cases.

输出
For each case,if there exists some satisfied string as described above,ouput the shortest length of such string,or just output -1 if we can’t find one.
样例输入
193411340
210623374112
193415041
193434013
193422026
样例输出
3
5
3
3

3

题意:

告诉哈希值,求原字符的位数。

思路:

枚举每位的上下界,down为ffffff80,up位ffffffff.只涉及到后7位。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef unsigned long long ll;
ll a[10],b[10];
int main()
{
    ll down=-(1<<7);
    ll up=(1<<7)-1;
    //printf("%x %x\n",down,up);
    a[0]=5381,b[0]=5381;
    for(int i=1;i<=8;i++)
    {
        a[i]=(a[i-1]*33)&down;
        b[i]=(b[i-1]*33)|up;
    }
    ll n;
    while(scanf("%I64u",&n)!=EOF)
    {
        int ans=-1;
        for(int i=0;i<=8;i++)
        {
            if(n>=a[i]&&n<=b[i])
            {
                ans=i;
                break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值