Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴一个125ms 的哈希#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; typedef unsigned long long ll; const int maxn=20004; const ll mul=131, p1=1000000007, p2=1000000009; ll hash1[maxn], hash2[maxn], pow1[maxn], pow2[maxn]; int N, K, temp, x, L, R, flag; namespace myMap{ int tot, plot[maxn], nxt[maxn], head[maxn], cnt[maxn]; ll val[maxn], val2[maxn]; void init(){ tot=1; memset(nxt, 0, sizeof(nxt)); memset(head, 0, sizeof(head)); memset(cnt, 0, sizeof(cnt)); } bool insert(int hash1, int hash2, int pos){ int v=hash1%(maxn-1); for(int t=head[v]; t; t=nxt[t])if(val[t]==hash1 && val2[t]==hash2) return ++cnt[t]>=K; nxt[tot]=head[v], val[tot]=hash1, val2[tot]=hash2, plot[tot]=pos, cnt[tot]=1, head[v]=tot++; return false; } } int main(){ scanf("%d%d",&N,&K); pow1[0]=pow2[0]=1; for(int i=1; i<maxn; i++) pow1[i]=pow1[i-1]*mul%p1, pow2[i]=pow2[i-1]*mul%p2; for(int i=1; i<=N; i++) scanf("%d",&temp), hash1[i]=(hash1[i-1]*mul+temp)%p1, hash2[i]=(hash2[i-1]*mul+temp)%p2; for(L=0, R=N-K+2; L+1<R; flag?L=x:R=x){ myMap::init(); x=(L+R)>>1; flag=false; for(int i=x; i<=N; i++){ ll t1=(hash1[i]+p1-(pow1[x]*hash1[i-x])%p1)%p1; ll t2=(hash2[i]+p2-(pow2[x]*hash2[i-x])%p2)%p2; if(myMap::insert(t1, t2, i)){ flag=true; break; } } } printf("%d\n", L); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator