stl::sort实现的后缀数组,超级慢。
变量名打错好几个导致WA好几次。
http://www.lydsy.com/JudgeOnline/problem.php?id=4278
/************************************************************** Problem: 4278 User: 1349367067 Language: C++ Result: Accepted Time:7900 ms Memory:13776 kb ****************************************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> usingnamespacestd; intn,m,a[200011]={},b[200011]={}; intr[800011]={},num; intSA[400011]={},RA[400011]={}; voidinit() { scanf("%d",&n); for(inti=1;i<=n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(inti=1;i<=m;i++) scanf("%d",&b[i]); for(inti=1;i<=n;i++) r[i]=a[i]; for(inti=1;i<=m;i++) r[n+i+1]=b[i]; r[n+1+m+1]=r[n+1]=1000000; num=n+m+2; } structTEM { intx,y,i; }tem[400011]; boolcmp(TEM a,TEM b) { if(a.x<b.x) return1; if(a.x>b.x) return0; if(a.y<b.y) return1; return0; } voidprintff() { for(inti=1;i<=num;i++) cout<<tem[i].x<<" "<<tem[i].y<<" "<<tem[i].i<<endl; cout<<"*************"<<endl; system("pause"); } voidprintfff() { for(inti=1;i<=num;i++) cout<<r[i]<<" "; cout<<endl<<"&&&&&&&&&&&&&"<<endl; system("pause"); } boolcheck(inti) { if(tem[i].x==tem[i-1].x&&tem[i].y==tem[i-1].y) returntrue; returnfalse; } voidsuffix_maker() { intt,T; for(inti=1;i<=num;i++) { tem[i].x=r[i];tem[i].y=0; tem[i].i=i; } sort(tem+1,tem+num+1,cmp); //printff(); T=0; for(inti=1;i<=num;i++) { if(!check(i)) T++; t=tem[i].i; r[t]=T; } //printfff(); for(inti=1;i<=num;i=i*2) { for(intj=1;j<=num;j++) { tem[j].x=r[j];tem[j].y=r[j+i]; tem[j].i=j; } sort(tem+1,tem+num+1,cmp); //printff(); T=0; for(intj=1;j<=num;j++) { if(!check(j)) T++; t=tem[j].i; r[t]=T; } //printfff(); } } voidwork() { intl=1,R=1; while(!(l>n&&R>m)) { //cout<<endl<<l-1<<" * "<<R-1<<endl; if(l>n) {printf("%d ",b[R++]);continue;} if(R>m) {printf("%d ",a[l++]);continue;} if(a[l]<b[R]) {printf("%d ",a[l++]);continue;} if(a[l]>b[R]) {printf("%d ",b[R++]);continue;} if(r[l]<r[n+R+1]) {printf("%d ",a[l++]);continue;} else {printf("%d ",b[R++]);continue;} } } intmain() { init(); suffix_maker(); //printfff(); work(); return0; }
时间: 2024-10-02 11:28:43