算法模板DataStructure.docx
- 文档编号:25768431
- 上传时间:2023-06-13
- 格式:DOCX
- 页数:62
- 大小:25.61KB
算法模板DataStructure.docx
《算法模板DataStructure.docx》由会员分享,可在线阅读,更多相关《算法模板DataStructure.docx(62页珍藏版)》请在冰豆网上搜索。
算法模板DataStructure
hash模板
unsignedintSDBMHash(char*str)
{
unsignedinthash=0;
while(*str)
{
hash=(*str++)+(hash<<6)+(hash<<16)-hash;
}
return(hash&0x7FFFFFFF);
}
unsignedintRSHash(char*str)
{
unsignedintb=378551;
unsignedinta=63689;
unsignedinthash=0;
while(*str)
{
hash=hash*a+(*str++);
a*=b;
}
return(hash&0x7FFFFFFF);
}
unsignedintJSHash(char*str)
{
unsignedinthash=1315423911;
while(*str)
{
hash^=((hash<<5)+(*str++)+(hash>>2));
}
return(hash&0x7FFFFFFF);
}
unsignedintPJWHash(char*str)
{
unsignedintBitsInUnignedInt=(unsignedint)(sizeof(unsignedint)*8);
unsignedintThreeQuarters=(unsignedint)((BitsInUnignedInt*3)/4);
unsignedintOneEighth=(unsignedint)(BitsInUnignedInt/8);
unsignedintHighBits=(unsignedint)(0xFFFFFFFF)<<(BitsInUnignedInt-OneEighth);
unsignedinthash=0;
unsignedinttest=0;
while(*str)
{
hash=(hash< if((test=hash&HighBits)! =0) { hash=((hash^(test>>ThreeQuarters))&(~HighBits)); } } return(hash&0x7FFFFFFF); } unsignedintELFHash(char*str) { unsignedinthash=0; unsignedintx=0; while(*str) { hash=(hash<<4)+(*str++); if((x=hash&0xF0000000L)! =0) { hash^=(x>>24); hash&=~x; } } return(hash&0x7FFFFFFF); } unsignedintBKDRHash(char*str) { unsignedintseed=131; unsignedinthash=0; while(*str) { hash=hash*seed+(*str++); } return(hash&0x7FFFFFFF); } unsignedintDJBHash(char*str) { unsignedinthash=5381; while(*str) { hash+=(hash<<5)+(*str++); } return(hash&0x7FFFFFFF); } unsignedintAPHash(char*str) { unsignedinthash=0; inti; for(i=0;*str;i++) { if((i&1)==0) { hash^=((hash<<7)^(*str++)^(hash>>3)); } else { hash^=(~((hash<<11)^(*str++)^(hash>>5))); } } return(hash&0x7FFFFFFF); } KMP模板 charW[10010],T[1000010]; intnext[10010]; intwlen,tlen; inlinevoidkmp(){ inti,k,j,ans; next[0]=-1;i=0;k=-1; while(i if(k==-1||W[k]==W[i])next[++i]=++k; elsek=next[k]; } i=-1;j=-1;ans=0; while(i if(j==-1||W[j]==T[i]){ i++; j++; } elsej=next[j]; if(j==wlen)ans++; } printf("%d\n",ans); } intmain(){ intt; scanf("%d",&t); while(t--){ scanf("%s%s",W,T); wlen=strlen(W);tlen=strlen(T); kmp(); } } AC自动机 structnode{ node*fail; node*next[26]; intcount; node(){ fail=NULL; memset(next,NULL,sizeof(next)); count=0; } }*que[1000000],*root; charstr[100010]; voidinsert(char*s){ node*p=root; intnum,i=0; while(s[i]){ num=s[i]-'a'; if(p->next[num]==NULL)p->next[num]=newnode(); p=p->next[num]; i++; } p->count++; } voidbuild(){ inti,head=0,tail=1; node*p,*temp; root->fail=NULL; que[0]=root; while(head temp=que[head++]; for(i=0;i<26;i++){ if(temp->next[i]! =NULL){ if(temp==root)temp->next[i]->fail=root; else{ p=temp->fail; while(p! =NULL){ if(p->next[i]! =NULL){ temp->next[i]->fail=p->next[i]; break; } p=p->fail; } if(p==NULL)temp->next[i]->fail=root; } que[tail++]=temp->next[i]; } } } } voidsearch(){ inti=0,cnt=0,num; node*p=root,*temp; while(str[i]){ num=str[i]-'a'; while(p->next[num]==NULL&&p! =root)p=p->fail; p=p->next[num]; if(p==NULL)p=root; temp=p; while(temp! =root&&temp->count! =-1){ cnt+=temp->count; temp->count=-1; temp=temp->fail; } i++; } printf("%d\n",cnt); } intmain(){ intt,n; charkeyword[60]; scanf("%d",&t); while(t--){ scanf("%d",&n); root=newnode(); while(n--){ scanf("%s",keyword); insert(keyword); } build(); scanf("%s",str); search(); } } 后缀数组 intrank[MAX],trank[MAX],f[MAX],sa[MAX],tsa[MAX],cnt[MAX],h[MAX],dp[MAX][13],p[13]; intn; charstr[MAX]; boolequal(inttmp[],inta,intb,intk){ if(a+k>=n&&b+k>=n)returntmp[a]==tmp[b]; if(a+k return0; } voidcreat_suffix_array(){ inti,k; memset(cnt,0,sizeof(cnt)); for(i=0;i for(i=1;i<=400;i++)cnt[i]+=cnt[i-1]; for(i=n-1;i>=0;i--)sa[--cnt[str[i]]]=i; for(rank[sa[0]]=0,i=1;i rank[sa[i]]=rank[sa[i-1]]; if(str[sa[i]]! =str[sa[i-1]])rank[sa[i]]++; } for(k=1;k for(i=0;i for(i=n-1;i>=0;i--) if(sa[i]>=k)tsa[--cnt[rank[sa[i]-k]]]=sa[i]-k; for(i=n-k;i for(trank[tsa[0]]=0,i=1;i trank[tsa[i]]=trank[tsa[i-1]]; if(! equal(rank,tsa[i],tsa[i-1],k))trank[tsa[i]]++; } for(i=0;i } } voidcreat_height(){ inti,j,k; for(k=0,i=0;i if(rank[i]==n-1)h[rank[i]]=k=0; else{ if(k)k--; j=sa[rank[i]+1]; for(;i+k h[rank[i]]=k; } } } voidinit_rmq(){ inti,j; for(i=0;i intt=(int)(log((double)(n-1))/log(2.0)); for(i=1;i<=t;i++) for(j=0;j if(j+(1<=n)break; dp[j][i]=min(dp[j][i-1],dp[j+(1<<(i-1))][i-1]); } } intquery(inta,intb){ intta=rank[a],tb=rank[b]; if(ta>tb)ta^=tb,tb^=ta,ta^=tb; intt=(int)(log((double)(tb-ta))/log(2.0)); returnmin(dp[ta][t],dp[tb-(1< } intmain(){ intlen,i,ans,l,r,k,r1,r2,pos; for(p[0]=1,i=1;i<=11;i++)p[i]=p[i-1]<<1; while(~scanf("%s",str)){ len=strlen(str); str[len]='#'; n=len+len+1; for(i=len+1;i str[n]=0; creat_suffix_array(); creat_height(); init_rmq(); ans=0; for(i=0;i if(i){ k=query(i,n-i); if(ans<2*k)ans=2*k,pos=i-k; } k=query(i,n-i-1); if(ans<2*k-1)ans=2*k-1,pos=i-k+1; } for(i=pos;i printf("\n"); } return0; } //堆模板 structnode{ intid,w; charstr[12]; }; classheap{ private: nodeque[size]; intmax; intpos[size]; public: voidreset(){ max=0; } nodeget(intnum){ returnque[pos[num]]; } boolcheck(nodetemp,nodemid){ return(temp.w>mid.w)||(temp.w==mid.w&&temp.id } voidpush(nodetemp){ inti; if(max==0){ que[0]=temp; pos[temp.id]=0; } else{ for(i=max;i>0;i=(i-1)/2){ if(check(que[(i-1)/2],temp))break; que[i]=que[(i-1)/2]; pos[que[i].id]=i; } que[i]=temp; pos[que[i].id]=i; } max++; } nodetop(){ returnque[0]; } voiddele(intnum){ inttemp=pos[num]; nodemid; max--; que[temp]=que[max]; pos[que[temp].id]=temp; mid=que[temp]; inti,child; i=temp; child=2*i+1; while(child if(child if(check(mid,que[child]))break; else{ que[i]=que[child]; pos[que[i].id]=i; i=child; child=2*i+1; } } que[i]=mid; pos[que[i].id]=i; } voidchange(intnum,intweight){ inttemp=pos[num]; if(weight>que[temp].w){ que[temp].w=weight; up(temp); } elseif(weight que[temp].w=weight; low(temp); } } voidup(intnum){ inti; nodetemp; temp=que[num]; for(i=num;i>0;i=(i-1)/2){ if(check(que[(i-1)/2],temp))break; que[i]=que[(i-1)/2]; pos[que[i].id]=i; } que[i]=temp; pos[que[i].id]=i; } voidlow(intnum){ nodetemp; temp=que[num]; intchild,i; i=num; child=2*i+1; while(child if(child if(check(temp,que[child]))break; else{ que[i]=que[child]; pos[que[i].id]=i; i=child; child=2*i+1; } } que[i]=temp; pos[que[i].id]=i; } boolempty(){ returnmax==0; } }H; 线段树求逆序数 intT[4500000]; intans; voidcreat(intpos,intl,intr){ T[pos]=0; if(l==r)return; intmid=(l+r)>>1; creat(pos*2,l,mid); creat(pos*2+1,mid+1,r); } voidinsert(intpos,intl,intr,inta){ T[pos]++; if(l==r)return; intmid=(l+r)>>1; if(a<=mid){ ans+=T[pos*2+1]; insert(pos*2,l,mid,a); } elseinsert(pos*2+1,mid+1,r,a); } intmain(){ intn,a,i; scanf("%d",&n); creat(1,1,n); ans=0; for(i=1;i<=n;i++){ scanf("%d",&a); insert(1,1,n,a); } printf("%d\n",ans); } 树状数组求逆序数 typedeflonglongll; constllsize=500010; lln; structnode{ llnum; llrank; }rec[size]; llres[size],vis[size]; boolcmp(constnodea,constnodeb){ returna.num } lllowbit(llnum){ returnnum&(-num); } voidins(llnum){ lltemp=num; while(temp>0){ vis[temp]++; temp-=lowbit(temp); } } llsum(llnum){ lltemp=num; llans=0; while(temp<=n){ ans+=vis[temp]; temp+=lowbit(temp); } returnans; } intmain(){ llans,i,j; while(scanf("%lld",&n),n){ for(i=0;i<=n-1;i++){ scanf("%lld",&rec[i].num); rec[i].rank=i; } sort(rec,rec+n,cmp); for(i=0;i<=n-1;i++){ rec[i].num=i+1; } for(i=0;i<=n-1;i++){ res[rec[i].rank]=rec[i].num; } ans=0; memset(vis,0,sizeof(vis)); for(i=0;i<=n-1;i++){ ans+=sum(res[i]); ins(res[i]); } printf("%lld\n",ans); } } 线段树+扫描线+离散化求面积 structnode{ intx,ly,hy,judge; node(){} node(inta,intb,intc,intd): x(a),ly(b),hy(c),judge(d){} booloperator<(constnodea)const{ returnx } }rec[2010]; structgg{ inty,rank; gg(){} gg(inta,intb): y(a),rank(b){} booloperator<(constgga)const{ returny } }m[2010]; intmap[2010]; structtree{ intcover,len; tree(){} tree(inta,intb): cover(a),len(b){} }T[10000]; voidcreat(intpos,intl,intr){ T[pos]=tree(0,0); if(l==r)return; intmid=(l+r)>>1; creat(pos*2,l,mid); creat(pos*2+1,mid+1,r); } voiddeal(intpos,intl,intr){ if(T[pos].cover){ T[pos].len=map[r+1]-map[l]; return; } if(l==r)T[pos].len=0; elseT[pos].len=T[pos*2].len+T[pos*2+1].len; } voidinsert(intpos,intl,intr,intstart,intend,intoper){ if(l==start&&r==end)T[pos].cover+=oper; else{ intmid=(l+r)>>1; if
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法 模板 DataStructure