pascal 中ansistring与string 的问题

来源:百度知道 编辑:UC知道 时间:2024/08/22 11:29:48
程序中把string改成ansistring就会出现越界错误
程序如下
program alien;

var
s,zidian,se:string;
i,j:longint;
max,n,l,r:int64;
f,shunxu:array[0..10000] of longint;
begin

readln(zidian);
for i:=1 to 26 do shunxu[ord(zidian[i])]:=i;

readln(se); l:=1;

while l<length(se) do
begin
r:=l;max:=0;
while (se[r]<>' ')and(r<=length(se))
do inc(r);
s:='';
s:=copy(se,l,r-l+1);

n:=length(s);for i:=1 to n do f[i]:=0;f[n]:=1;
for i:=n-1 downto 0 do
begin f[i]:=1;
for j:=n downto i+1 do
if (shunxu[ord(s[i])]<=shunxu[ord(s[j])])and(f[j]+1>f[i]) then f[i]:=f[j]+1;
end;
for i:=1 to n do if f[i]>max then max:=f[i];
write(max);
l:=r+1;
end;
writeln;
end.

输入
abcdefghijklmnopqrstuvwxyz
abcd efg hhh ihg
输出

问题很简单,ansistring是从0位开始的,string是从1位开始的,就是说比如a是ansistring,那么你输入一个字符串,第一位就存在a[0]里,如果a是string那么第一位就存在a[1]里,所以你的数组就不对了,把所有都偏移一位就好了,其实就是string类型中的第0位就是a[0]存的是整个的长度,而ansistring不存这个。

首先
while (se[r]<>' ')and(r<=length(se))
这句有错
不能先判断是否为空,如果r>length(se)的话ansistring会报错
应该交换一下while (r<=length(se)) and (se[r]<>' ')

第二
for i:=n-1 downto 0 do
begin f[i]:=1;
这句有错
改成for i:=n-1 downto 1 do
因为后面用到了s[i]
i是0的话ansistring也会报错

至于楼上说的 我试过了 好像没有这种事吧?