用栈去模拟
弹栈出栈就做完了
当时还没转c
用p写的
program aaa;
var sl,n,m,i,j,k,tb,tc,x:longint;
a:array[1..60000] of string;
b,c,t:array[1..1000000] of longint;
s,st:ansistring;
flag:boolean;
begin
readln(k);
for x:=1 to k do
begin
fillchar(t,sizeof(t),0);
fillchar(c,sizeof(c),0);
fillchar(b,sizeof(b),0);
readln(s);
sl:=length(s);
while s[sl]=' ' do dec(sl);
i:=1;st:='';m:=0;
n:=0;tb:=0;tc:=0;
while i<=sl do
begin
while (s[i]<>'(') and (s[i]<>')') and (s[i]<>',') and(i<=sl) do
begin
flag:=false;
st:=st+s[i];
i:=i+1;
end;
if (st<>'') then begin n:=n+1;a[n]:=st;tb:=tb+1;b[tb]:=n;m:=m+1;t[m]:=n;
st:='';
end;
if s[i]='(' then
begin
tc:=tc+1;c[tc]:=tb;
end;
if s[i]=')' then
begin
tb:=c[tc];
m:=m+1;
t[m]:=b[tb];
tc:=tc-1;
end;
if s[i]=',' then
begin
m:=m+1;
t[m]:=b[c[tc]];
end;
inc(i);
end;
writeln(n);
for i:=1 to n do
writeln(a[i]);
for i:=1 to m-1 do writeln(t[i],' ',t[i+1]);
writeln;
end;
end.
本文详细介绍了如何使用栈数据结构来解析和操作包含括号、逗号和非括号字符的字符串,通过读取输入、识别并处理不同类型的字符,最终输出解析后的字符串及其结构。
1048

被折叠的 条评论
为什么被折叠?



