How long can a Pascal string be?

Write here if you have problems with your Pascal source code

Moderator: Board moderators

How long can a Pascal string be?

I'm trying to solve 10324, using a string to read in the characters.
Using my compiler (Delphi 5) i can get it right using 1000000 characters,
but i keep getting WA from the judge.

Are Pascal strings limited to 255B (1 byte string lenght) or 2GB (2 byte string lenght - ANSI String)?

[pascal]
program P10324;

type TArray = array[1..1000000] of Boolean;

var Niza:TArray;
i,Dim,Jen,Dva,n:Integer;
NizaLen:Integer;
Go:Boolean;
Line:string;

var i:Integer;
begin
FillChar(ANiza,SizeOf(ANiza),False);
for i:=2 to Length(Line) do begin
if Line[i]<>Line[i-1] then ANiza[i-1]:=True;
end;
Len:=Length(Line)-1;
end;

function IsSame(Niza:TArray;Len,Start,Kraj:Integer):string;
var i,tmp:Integer;
rez:Boolean;
begin
if Start>Kraj then begin
tmp:=Start;
Start:=Kraj;
Kraj:=tmp;
end;
if Kraj>Len then Kraj:=Len;
if Kraj<0 then Kraj:=0;
if Start>Len then Start:=Len;
if Start<0 then Start:=0;
i:=Start+1;
rez:=True;
while (i<=Kraj) and rez do begin
if Niza[i] then rez:=False;
Inc(i);
end;
if rez then IsSame:='Yes' else IsSame:='No';
end;

begin
n:=1;Go:=True;
while (not Eof) and (Go) do begin
Go:=(Line<>'');
if Go then begin
Writeln('Case ',n,':');
Inc(n);
for i:=1 to Dim do begin
Writeln(IsSame(Niza,NizaLen,Jen,Dva));
end;
end;
end;
end.
[/pascal]

Btw, in the program I (think I) check for both eof and blank line - it's in the problem description, so i guess that's not the problem
SWeko has spoken
sweko
New poster

Posts: 7
Joined: Fri Apr 19, 2002 4:05 pm

If I remeber correctly, Pascal's string was of structure:
| 1 byte | up to 255 bytes |
The first byte is used for string length.

Delphi's string length was determined by first 4 bytes giving you 2GB strings at max. I remeber reading about it some long time ago.

Ivor
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
Ivor
Experienced poster

Posts: 147
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

You can use longstrings too by specifying the length:
[pascal]type longstring=string(1000000);[/pascal]
The default is string(255), but I don't know if it's a standard pascal shortstring (i.e. with the length in byte 0).

Since this is quite not-standard, most compilers will not know what to do with this. My Free Pascal compiler uses square brackets, so I use the clause:
[pascal]{\$IFNDEF ONLINE_JUDGE}
type longstring=string[1000000];
{\$ELSE}
type longstring=string(1000000);
{\$ENDIF}
[/pascal]
so it works on both compilers.
xenon
Learning poster

Posts: 100
Joined: Fri May 24, 2002 10:35 am
Location: Scheveningen, Holland

define the size

Hi,
you can also set the size of the string, like
yourstring: string(10000);

A string is not more than an array of char
Last edited by ACoimbra on Thu Apr 10, 2003 3:50 pm, edited 1 time in total.
ACoimbra
New poster

Posts: 14
Joined: Thu Apr 10, 2003 1:59 pm
Location: Coimbra, Portugal

I think with GPU a string can have max length of 32767, but I'm not too sure about that with FreePascal. Declaration is done with curve brackets:

var s : string(32767);
junjieliang
Experienced poster

Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

With the new free pascal compiler you can use the string type ansistring that can be virtualy as long as you like.
[pascal]var
mystring:ansistring;
begin
end.[/pascal]reads until it encounters an EOLN, even if it has to read thousands or millions of characters.
You can also use the procedure setlength() to explicitly allocate memory, like:[pascal]setlength(mystring,2000000000);[/pascal]
Memory is allocated dynamically.
There are some restrictions; read the FPC documentation for details.

little joey
Guru

Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

delphi mode

you can activate delphi mode, so you will have strings with dynamic size
ACoimbra
New poster

Posts: 14
Joined: Thu Apr 10, 2003 1:59 pm
Location: Coimbra, Portugal

simply use ansistring instead of string...
it gives u a string with dynamic size

var
s : ansistring;
raymond85
New poster

Posts: 21
Joined: Tue Jul 01, 2003 9:26 am
Location: Hong Kong

hmn

Isn't that too much slow?
ACoimbra
New poster

Posts: 14
Joined: Thu Apr 10, 2003 1:59 pm
Location: Coimbra, Portugal

Re: hmn

ACoimbra wrote:Isn't that too much slow?

Of coz, if you are storing a long string that uses a lot memory, I think it might slow down the processing speed. By the way I think it's acceptable, I used it instead of array of character many times when doing ACM problems and got AC without much different on the processing speed.
raymond85
New poster

Posts: 21
Joined: Tue Jul 01, 2003 9:26 am
Location: Hong Kong

Re: How long can a Pascal string be?

yes you might use unsistring because of it's dinamic size, though it will make it slower.
pecijackson
New poster

Posts: 3
Joined: Tue Jan 31, 2012 7:05 am

Re: How long can a Pascal string be?

I have no idea but get lots of help from your page.. knowledgeable sharing you have done....
sindrella
New poster

Posts: 1
Joined: Thu Mar 07, 2013 8:43 am