2015-7-21 模板练习
1.ac自动机
1A 妈妈呀
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 using namespace std; 10 const int maxn=1000000+10,inf=-1u>>1,sig=2; 11 struct node{ 12 node*tx[sig],*fail;int cnt;node(){cnt=0;} 13 }ac[maxn],*nodecnt=ac,*root=nodecnt++; 14 void insert(char*s){ 15 node*x=root; 16 for(int i=0;s[i];i++){ 17 int c=s[i]-'a'; 18 if(!x->tx[c])x->tx[c]=nodecnt++; 19 x=x->tx[c]; 20 }x->cnt++;return; 21 } 22 void getfail(){ 23 queue<node*>Q;for(int c=0;c<sig;c++)if(root->tx[c])Q.push(root->tx[c]); 24 while(!Q.empty()){ 25 node*x=Q.front();Q.pop(); 26 for(int c=0;c<sig;c++)if(x->tx[c]){ 27 node*p=x->fail;while(p&&!p->tx[c])p=p->fail;if(!p)p=root; 28 x->tx[c]->fail=p->tx[c]?p->tx[c]:root;Q.push(x->tx[c]); 29 } 30 }return; 31 } 32 int query(char*s){ 33 node*x=root;int sum=0; 34 for(int i=0;s[i];i++){ 35 int c=s[i]-'a'; 36 while(x&&!x->tx[c])x=x->fail; 37 x=x->tx[c];if(x->cnt)sum++; 38 }return sum; 39 } 40 inline int read(){ 41 int x=0,sig=1;char ch=getchar(); 42 while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();} 43 while(isdigit(ch)) x=10*x+ch-'0',ch=getchar(); 44 return x*=sig; 45 } 46 inline void write(int x){ 47 if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x; 48 int len=0,buf[15];while(x) buf[len++]=x%10,x/=10; 49 for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return; 50 } 51 char s[maxn]; 52 void init(){ 53 scanf("%s",s);insert(s);getfail(); 54 scanf("%s",s);write(query(s)); 55 return; 56 } 57 void work(){ 58 return; 59 } 60 void print(){ 61 return; 62 } 63 int main(){ 64 init();work();print();return 0; 65 }
2.FFT
1RE line42:L<L1<<1||L<L2<<1 顺序。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 using namespace std; 10 const int maxn=100000+10,maxt=300000+10; 11 const double pi=acos(-1.0); 12 struct fft{ 13 struct cx{double r,i;cx(double _r=0.0,double _i=0.0){r=_r;i=_i;} 14 cx operator+(const cx&b){return cx(r+b.r,i+b.i);} 15 cx operator-(const cx&b){return cx(r-b.r,i-b.i);} 16 cx operator*(const cx&b){return cx(r*b.r-i*b.i,r*b.i+i*b.r);} 17 }f[maxt];int len; 18 void init(int*s,int L,int len){ 19 this->len=len;for(int i=0;i<L;i++)f[i]=cx(s[L-i-1],0.0);return; 20 } 21 void change(){ 22 for(int i=1,j=len>>1;i<~-len;i++){ 23 if(i<j)swap(f[i],f[j]);int k=len>>1; 24 while(j>=k)j-=k,k>>=1;if(j<k)j+=k; 25 }return; 26 } 27 void cal(int tp){ 28 change();double tm=-tp*2*pi; 29 for(int i=2;i<=len;i<<=1){ 30 double tr=tm/i;cx wn(cos(tr),sin(tr)); 31 for(int j=0;j<len;j+=i){ 32 cx w(1.0,0.0);int tn=i>>1; 33 for(int k=j;k<j+tn;k++){ 34 cx u=f[k],t=w*f[k+tn]; 35 f[k]=u+t;f[k+tn]=u-t;w=w*wn; 36 } 37 } 38 }if(tp<0)for(int i=0;i<len;i++)f[i].r/=len;return; 39 } 40 }; 41 void mul(int*s,int*t,int L1,int L2,int*ans,int&L){ 42 L=1;while(L<L1<<1||L<L2<<1)L<<=1;static fft a,b; 43 a.init(s,L1,L);b.init(t,L2,L);a.cal(1);b.cal(1); 44 for(int i=0;i<L;i++)a.f[i]=a.f[i]*b.f[i];a.cal(-1); 45 for(int i=0;i<L;i++)ans[i]=(int)(a.f[i].r+0.5);return; 46 } 47 inline int read(){ 48 int x=0,sig=1;char ch=getchar(); 49 while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();} 50 while(isdigit(ch)) x=10*x+ch-'0',ch=getchar(); 51 return x*=sig; 52 } 53 inline void write(int x){ 54 if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x; 55 int len=0,buf[15];while(x) buf[len++]=x%10,x/=10; 56 for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return; 57 } 58 int s[maxn],t[maxn],ans[maxt],L1,L2,L; 59 void init(){ 60 L1=read()+1;L2=read()+1; 61 for(int i=0;i<L1;i++) s[i]=read(); 62 for(int i=0;i<L2;i++) t[i]=read(); 63 mul(s,t,L1,L2,ans,L); 64 return; 65 } 66 void work(){ 67 int lim=L1+L2-2; 68 for(L--;!ans[L]&&L&&L>lim;L--); 69 for(int i=L;i>=0;i--) write(ans[i]),PAU; 70 return; 71 } 72 void print(){ 73 return; 74 } 75 int main(){init();work();print();return 0;}
3.splay
1WA line80:瞎join了QAQ......
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 #define CH for(int d=0;d<2;d++) if(ch[d]) 10 #define lson x->ch[0],L,M-1 11 #define rson x->ch[1],M+1,R 12 using namespace std; 13 const int maxn=100000+10; 14 struct node{ 15 node*ch[2],*fa;char x;bool rev;int siz;node(){rev=false;siz=1;x='&';} 16 void revt(){swap(ch[0],ch[1]);rev^=1;return;} 17 void update(){siz=1;CH{siz+=ch[d]->siz;}return;} 18 void down(){if(rev){CH{ch[d]->revt();}rev=false;}return;} 19 }Splay[maxn],*nodecnt=Splay,*root; 20 int parent(node*x,node*&y){return (y=x->fa)?y->ch[1]==x?1:y->ch[0]==x?0:-1:-1;} 21 void rotate(node*x){ 22 node*y,*z;int d1=parent(x,y),d2=parent(y,z); 23 if(y->ch[d1]=x->ch[d1^1])y->ch[d1]->fa=y; 24 y->fa=x;x->fa=z;x->ch[d1^1]=y; 25 if(d2!=-1)z->ch[d2]=x; 26 y->update();return; 27 } 28 void pushdown(node*x){ 29 static node*s[maxn];int top=0; 30 for(node*y;;x=y){ 31 s[top++]=x;y=x->fa; 32 if(!y||(y->ch[0]!=x&&y->ch[1]!=x))break; 33 }while(top--)s[top]->down();return; 34 } 35 node*splay(node*x){ 36 pushdown(x);node*y,*z;int d1,d2; 37 while(true){ 38 if((d1=parent(x,y))<0)break; 39 if((d2=parent(y,z))<0){rotate(x);break;} 40 if(d1==d2)rotate(y),rotate(x); 41 else rotate(x),rotate(x); 42 }x->update();return x; 43 } 44 char A[maxn];int n; 45 void build(node*&x=root,int L=0,int R=n){ 46 if(L>R)return;x=nodecnt++;int M=L+R>>1;x->x=A[M]; 47 build(lson);build(rson); 48 if(x->ch[0])x->ch[0]->fa=x; 49 if(x->ch[1])x->ch[1]->fa=x;x->update();return; 50 } 51 void print(node*x){ 52 if(!x)return; 53 x->down(); 54 print(x->ch[0]); 55 putchar(x->x); 56 print(x->ch[1]); 57 return; 58 } 59 node*find(node*x,int k){ 60 x->down();int kth=x->ch[0]?x->ch[0]->siz+1:1; 61 if(kth==k)return x; 62 if(k<kth)return find(x->ch[0],k); 63 return find(x->ch[1],k-kth); 64 } 65 node*findlas(node*x){ 66 splay(x)->down();while(x->ch[1])x=x->ch[1],x->down();return x; 67 } 68 void split(node*&x,node*&y,int a){ 69 if(!a){y=x;x=NULL;return;}x=find(x,a);splay(x); 70 y=x->ch[1];x->ch[1]=NULL;if(y)y->fa=NULL;x->update();return; 71 } 72 void split(node*&x,node*&y,node*&z,int a,int b){ 73 split(x,z,b);split(x,y,a-1);return; 74 } 75 void join(node*&x,node*y){ 76 if(!x){x=y;return;}if(!y)return;x=findlas(x);splay(x); 77 x->ch[1]=y;if(y)y->fa=x;x->update();return; 78 } 79 void join(node*&x,node*y,node*z){ 80 join(y,z);join(x,y);return; 81 } 82 void reverse(int L,int R){ 83 node*x,*y;split(root,x,y,L,R);x->revt();join(root,x,y);return; 84 } 85 inline int read(){ 86 int x=0,sig=1;char ch=getchar(); 87 while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();} 88 while(isdigit(ch)) x=10*x+ch-'0',ch=getchar(); 89 return x*=sig; 90 } 91 inline void write(int x){ 92 if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x; 93 int len=0,buf[15];while(x) buf[len++]=x%10,x/=10; 94 for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return; 95 } 96 void init(){ 97 scanf("%s",A);n=strlen(A)-1; 98 build(); 99 return; 100 } 101 void work(){ 102 int x,y,Q=read(); 103 while(Q--){ 104 x=read();y=read();reverse(x,y); 105 } 106 return; 107 } 108 void print(){ 109 print(root); 110 return; 111 } 112 int main(){ 113 init();work();print();return 0; 114 }
4.重心
1WA line41:f[CG]=好大
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 using namespace std; 10 const int maxn=100000+10,inf=-1u>>1; 11 struct ted{int x,y;ted*nxt;}adj[maxn<<1],*fch[maxn],*ms=adj; 12 void add(int x,int y){ 13 *ms=(ted){x,y,fch[x]};fch[x]=ms++; 14 *ms=(ted){y,x,fch[y]};fch[y]=ms++; 15 return; 16 } 17 int CG=2,siz[maxn],f[maxn],n; 18 void findcg(int x,int fa){ 19 int mxs=-inf;siz[x]=1; 20 for(ted*e=fch[x];e;e=e->nxt){ 21 int v=e->y;if(v!=fa){ 22 findcg(v,x);siz[x]+=siz[v]; 23 mxs=max(mxs,siz[v]); 24 } 25 }f[x]=max(mxs,n-siz[x]); 26 if(f[x]<f[CG])CG=x; 27 else if(f[x]==f[CG])CG=min(x,CG);return; 28 } 29 inline int read(){ 30 int x=0,sig=1;char ch=getchar(); 31 while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();} 32 while(isdigit(ch)) x=10*x+ch-'0',ch=getchar(); 33 return x*=sig; 34 } 35 inline void write(int x){ 36 if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x; 37 int len=0,buf[15];while(x) buf[len++]=x%10,x/=10; 38 for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return; 39 } 40 void init(){ 41 n=read();f[CG]=inf; 42 for(int i=1;i<n;i++)add(read(),read()); 43 findcg(1,0); 44 write(CG); 45 return; 46 } 47 void work(){ 48 return; 49 } 50 void print(){ 51 return; 52 } 53 int main(){ 54 init();work();print();return 0; 55 }