当前位置:编程学习 > C/C++ >>

将下面C++代码翻译成Pascal代码

#define maxn 1000001
int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) ws[i]=0;
for(i=0;i<n;i++) ws[x[i]=r[i]]++;
for(i=1;i<m;i++) ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p)
{
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[y[i]];
for(i=0;i<m;i++) ws[i]=0;
for(i=0;i<n;i++) ws[wv[i]]++;
for(i=1;i<m;i++) ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return;
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=0;
for(i=1;i<=n;i++) rank[sa[i]]=i;
for(i=0;i<n;height[rank[i++]]=k)
for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
return;
}
int RMQ[maxn];
int mm[maxn];
int best[20][maxn];
void initRMQ(int n)
{
int i,j,a,b;
for(mm[0]=-1,i=1;i<=n;i++)
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
for(i=1;i<=n;i++) best[0][i]=i;
for(i=1;i<=mm[n];i++)
for(j=1;j<=n+1-(1<<i);j++)
{
a=best[i-1][j];
b=best[i-1][j+(1<<(i-1))];
if(RMQ[a]<RMQ[b]) best[i][j]=a;
else best[i][j]=b;
}
return;
}
int askRMQ(int a,int b)
{
int t;
t=mm[b-a+1];b-=(1<<t)-1;
a=best[t][a];b=best[t][b];
return RMQ[a]<RMQ[b]?a:b;
}
int lcp(int a,int b)
{
int t;
a=rank[a];b=rank[b];
if(a>b) {t=a;a=b;b=t;}
return(height[askRMQ(a+1,b)]);
}
答案:const maxn=1000001;
type arr=array[0..maxn] of integer;
var wa,wb,wv,ws,rank,height,RMQ,mm:arr;
best:array[0..19] of arr;
function cmp(r:arr;a,b,l:integer):boolean;
begin
exit((r[a]=r[b])and(r[a+l]=r[b+l]));
end;
procedure da(r,sa:arr;n,m:integer);
var i,j,p:longint;
x,y,t:arr;
begin
x:=wa; y:=wb;
for i:=0 to m-1 do ws[i]:=0;
for i:=0 to n-1 do inc(ws[x[i]=r[i]]);
for i:=n-1 downto 0 do begin
dec(ws[x[i]]);
sa[ws[x[i]]]:=i;
end;
j:=1;
for p:=1 to n-1 do begin
j:=j*2;
m:=p;
p:=0;
for i:=n-j to n-1 do begin y[p]:=i; inc(p); end;
for i:=0 to n-1 do if sa[i]>=j then begin y[p]=sa[i]-j; p:=p+1; end;
for i:=0 to n-1 do wv[i]=x[y[i]];
for i:=0 to m-1 do ws[i]:=0;
for i:=0 to n-1 do inc(ws[wv[i]]);
for i:=1 to m-1 do ws[i]:=ws[i]+ws[i-1];
for i:=n-1 downto 0 do begin dec(ws[wv[i]]); sa[ws[wv[i]]]=y[i];
t:=x; x:=y; y:=t; p:=1; x[sa[0]]:=0;
for i:=1 to n-1 do
if cmp(y,sa[i-1],sa[i],j)=true then x[sa[i]]:=p-1 else begin x[sa[i]]:=p; inc(p); end;
end;
exit;
end;
procedure calheight(r,sa:arr;n:integer);
var i,j,k:integer;
begin
k:=0;
for i:=1 to n do rank[sa[i]]:=i;
for i:=0 to n-1 do height[rank[i]]:=k;
if k>0 then k:=k-1 else k:=0;
j:=sa[rank[i]=1];
while not(r[i+k]==r[j+k]) do inc(k);
exit;
end;
procedure initRMQ(n:integer);
var i,j,a,b:integer;
begin
mm[0]:=-1;
for i:=1 to n-1 do if ((i and (i-1))=0) then mm[i]=mm[i-1]+1 else mm[i]:=mm[i-1];
for i:=1 to n do best[0][i]:=i;
for i:=1 to mm[n] do
for j:=1 to n+1-(1 << i) do begin
a:=best[i-1][j];
b:=best[i-1][j+(1<<(i-1))];
if (RMQ[a]<RMQ[b]) best[i][j]:=a else best[i][j]:=b;
end;
end;
function askRMQ(a,b:integer)
var t:itneger;
begin
y:=mm[b-a+1];
b:=b-(1<<t)-1;
a:=best[t][a]; b=best[b];
if RMQ[a]<RMQ[b] then exit(a) else exit(b);
end;
function lcp(a,b:itneger);
var t:integer;
begin
a:=rank[a]; b:=rank[b];
if (a>b) then begin t:=a; a:=b; b:=t; end;
exit(height[askRMQ(a+1,b)]);
end;

注:你这段代码无主函数。

上一个:C++ Builder通过WMI获取系统信息
下一个:关于C++ TCP/IP通信的问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,