4278: [Ontak2015]tasowanie time limit:10 Sec Memory limit:256 MB
submit:164 solved:80
[Submit] [Status] [Discuss] Description given two numbers of strings A and B, by merging A and B to get a new number string T, find the smallest dictionary order T. The first line of input contains a positive integer n (1<=n<=200000), which represents the length of a string. The second line contains n positive integers, where the number of I represents a[i] (1<=a[i]<=1000). The third line contains a positive integer m (1<=m<=200000) that represents the length of the B-string. The four lines contain m positive integers, where the number of I represents b[i] (1<=b[i]<=1000). Output outputs a row containing n+m positive integers, which are the least-ordered T-strings of the dictionary. Sample Input6
1 2 3 1 2 4
7
1 2 2 1 3 4 3Sample Output1 1 2 2 1 2 3 1 2 3 4 3 4
Hintsource
by Claris
Solution
One-eye-second suffix array naked question? Calculate the rank array to
Two-way merge output answer can ....
PS feeling Claris will not pass naked problems come up, so% of his practice, is a greedy
Code
#include <cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 400010intLEN,VAL[MAXN];intS[MAXN];intSA[MAXN];intWS[MAXN],WA[MAXN],WV[MAXN],WB[MAXN];intcmpint(RNintAintBintl) { returnr[a]==r[b]&&r[a+l]==r[b+l];}voidCAOint(RNint*sa,intNintm) { intp,*x=wa,*y=wb,*T; for(intI=0; i<m; i++) ws[i]=0; for(intI=0; i<n; i++) ws[x[i]=r[i]]++; for(intI=1; i<m; i++) ws[i]+=ws[i-1]; for(inti=n-1; i>=0; i--) sa[--ws[x[i]]]=i; P=1; for(intj=1; p<n; j*=2, m=p) {p=0; for(intI=n-j; i<n; i++) y[p++]=i; for(intI=0; i<n; i++)if(SA[I]>=J) y[p++]=sa[i]-J; for(intI=0; i<n; i++) wv[i]=X[y[i]]; for(intI=0; i<m; i++) ws[i]=0; for(intI=0; i<n; i++) ws[wv[i]]++; for(intI=1; i<m; i++) ws[i]+=ws[i-1]; for(inti=n-1; i>=0; i--) sa[--ws[wv[i]]]=Y[i]; T=x; X=y; y=t; p=1; x[sa[0]]=0; for(intI=1; i<n; i++) X[sa[i]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; } }intRANK[MAXN],HEIGHT[MAXN];voidCalheight (int(RNint*sa,intN) { intk=0; for(intI=1; i<=n; i++) rank[sa[i]]=i; for(intI=0; i<n; height[rank[i++]]=k) {k? k--:0; for(intj=sa[rank[i]-1]; R[I+K]==R[J+K]; k++);}}intn,m;intMain () {N=read (); for(intI=0; i<n; i++) S[i]=read (); s[n]=1001; M=read (); for(intI=1; i<=m; i++) S[i+n]=read (); s[n+m+1]=0; DA (S,sa,n+m+2,1002); Calheight (s,sa,n+m+1); intA=0, b=n+1; for(intI=0; i<=n+m; i++) { if(i==n)Continue; if(a==n) { for(intJ=b; j<=n+m; J + +) printf ("%d", S[j]); Break;} if(b==n+m+1) { for(intJ=a; j<n; J + +) printf ("%d", S[j]); Break;} if(Rank[a]<rank[b]) printf ("%d", S[a]), a++; Elseprintf"%d", S[b]), b++; } return 0;}
How to run so much faster than HS (hen) Y. But it was the first page of the card ....
"BZOJ-4278" Tasowanie suffix array + merge