Hash的应用

Hash其实对字符串的查找比较有很好的应用,例如将一个字符串变成一个数字,就像将“12345”转成十进制的12345

只不过这里不是每一为乘十而是乘以一个素数,例如97或者9973等,但是成大了以后会爆long long可以根据数据的大小自行确定基数,这里每一个字符串就被唯一表示为一个数字,重复的概率几乎没有,可以快速匹配字符串,这里的基数和如果需要mod的话,这两个值必须都是素数,但具体选几不一定得试试看看哪种组合冲突少

两道例题:

 

Hat’s Words

 

A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. 
You are to find all the hat’s words in a dictionary. 

Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words. 
Only one case. 

Output

Your output should contain all the hat’s words, one per line, in alphabetical order.

Sample Input

a
ahat
hat
hatword
hziee
word

Sample Output

ahat
hatword

分析:

判断一个字符串是否由两个别的串组成,这里本来考虑一个字符串由两个相同的字符串组成可不可以,但似乎想多了题里没特意强调这个,我们就暂且忽略不计

代码:

#include<stdio.h>
#include<vector>
#include<string.h>
#include<map>
using namespace std;
#define ll  unsigned long long
map<ll,bool>m;
ll kk=97;
ll getHash(char a[1000],int i,int j)
{
    ll sum=0;
    for(int k=i; k<=j; k++)
    {
        sum=sum*kk+(a[k]-'a')+1;
    }
    return sum;
}
char p[50003][1000];
int main()
{

    m.clear();
    int i=0;
    int len=0;
    while(~scanf("%s",&p[i]))
    {
        len=strlen(p[i]);
        ll o= getHash(p[i],0,len-1);
        m[o]=true;
        i++;
    }
    for(int h=0; h<i; h++)
    {
        len=strlen(p[h]);
        for(int l=0; l<len-1; l++)
        {
            if(m[getHash(p[h],0,l)]&&m[getHash(p[h],l+1,len-1)])
            {
                printf("%s\n",p[h]);
                break;
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值