请教神牛_字符串hash

    针对字符串hash 我早就听闻可以暴力的干一些事情。

比如 可以...

很多很多 实现O(n)求出 模式串在文本串出现的次数。

但是我不会这什么hash。

我会自然溢出字符串hash 嘿嘿 unsigned long long 溢出后可以 对2^32自动取%

采用p进制字符串hash 我想出现冲突的可能性是 99%

这道题呢 采用map 可以快速A但是hash 也是可以取得不错的效果的。

我想 可以随便搞了。sort稳重操作!

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<utility>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<vector>
#include<ctime>
#define INF 168430090
#define mod 19260817
#define ull unsigned long long
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc()
{
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void put(int x)
{
    x<0?putchar('-'),x=-x:0;
    int num=0;char ch[50];
    while(x)ch[++num]=x%10+'0',x/=10;
    num==0?putchar('0'):0;
    while(num)putchar(ch[num--]);
    putchar('\n');return;
}
const int MAXN=500,maxn=50002;
int n,ans,c[maxn];
char a[MAXN];
struct wy
{
    ull b;
    int id;
    friend int operator <(const wy &x,const  wy &y)
    {
        if(x.b==y.b)return x.id<y.id;
        return x.b<y.b;
    }
}t[maxn];
int main()
{
    //freopen("1.in","r",stdin);
    n=read();
    for(int i=1;i<=n;i++)
    {
        scanf("%s",a+1);
        int len=strlen(a+1);
        t[i].id=i;
        for(int j=1;j<=len;j++)t[i].b=t[i].b*mod+a[j];
    }
    sort(t+1,t+1+n);
    for(int i=2;i<=n;i++)if(t[i].b==t[i-1].b)c[t[i].id]=1;
    for(int i=1;i<=n;i++)if(c[i]==1)put(i);
    return 0;
}

我居北海君南海,寄雁传书谢不能。

posted @ 2019-02-13 21:05  chdy  阅读(148)  评论(0编辑  收藏  举报