Pascal程序笔迹:弧形判断
来源:互联网 发布:tst代理淘宝开店规则 编辑:程序博客网 时间:2024/05/29 04:37
<Pascal笔迹>>>弧形判断
作者:A_Ender
[题目介绍]
判断一个括号字符串表达式是否正确YES or NO。(其中'<>'是最大,其后依是'{}','[]','()')
[样例输入,输出]
输入1:<{[()]}>{()}<[]>
输出1:YES
输入2:{[()][{}]}<>
输出2:NO
输入3:{}{}{(})[()]
输出3:NO
[题目数据]
length(s)<=50
{本题套路}
有2种错误方式:
{1}是包含错误,小的包大的。如:[{}],([])等。
{2}是对号位置错误或根本不存在。如:([)],[()等。
{算法详解}
本人有两种算法(当然推荐第2种)
{1}递归算法
将整个字符串分为2部分 讲解样例:<[]()>{}
head是<,tail是}。其中head的对号位置是是6,tail的对号的位置是7。
这里将整个字符串分为了2部分,<[]()>和{},{}这部分可以直接不找了。
<[]()>可以分成2部分,head是[,tail是)。分别找到对号。
最后找到tail-head=1就去可以了。
注意:
1.提前观察包含是否错误。
2.如果本head和tail都正确,进入head+1,tail-1。
3.找对号要分别左往右,右往左。
4.找对号时每找到一个同号j+1,找到一个对号j-1,直到j=-1。(你懂得)
5.如果head-tadil-1是基数说明本段是错误的,NO。(因为括号数不可能是基数,如<[>)
6.如果找不到对号就直接输出NO,然后halt。
{2}栈
每加入一个左号('<','{','[','(')就去把它的对号删掉。
注意:
1.提前观察包含是否错误。
2.最后还省符号,则NO。
{算法演示}
样例:{[]}<>
{1}递归算法:
1.级别没有错误
2.head=1 tail=6
{1段第1部分head=1,tail=4
{2段第1部分head=2,tail=3,回给YES}
1段第2部分head=5,tail=6,再次回给YES}
过程中没有NO,并且都监测到YES
{2}栈
1.级别没有错误
2.{[]}<>
3.[]<>
4.<>
5.空
<代码实现>
{1}递归算法:
var
s:string;
i:longint;
procedure sc;
begin
writeln('NO');
halt;
end;
function hj(s:string):longint; //hua jian
begin
if (s='(')or(s=')') then
hj:=1;
if (s='[')or(s=']') then
hj:=2;
if (s='{')or(s='}') then
hj:=3;
if (s='<')or(s='>') then
hj:=4;
end;
function max(s,s1:string;n:longint):longint; //pan duan
var
min:longint;
begin
max:=hj(s);
min:=hj(s1);
if n=1 then
begin
if max>=min then
exit(1)
else
exit(0);
end;
if n=2 then
begin
if max>min then
exit(1)
else
exit(0);
end;
end;
function pd(i:longint):longint;
begin
if (s[i]='[')or(s[i]='(')or(s[i]='<')or(s[i]='{') then
exit(1)
else
exit(2);
end;
function f(s:string):string; //zhuan hua
begin
if s='(' then
f:=')';
if s=')' then
f:='(';
if s='[' then
f:=']';
if s=']' then
f:='[';
if s='{' then
f:='}';
if s='}' then
f:='{';
if s='<' then
f:='>';
if s='>' then
f:='<';
end;
function zdz(s:string;ll:longint):longint;
var
i,l,j:longint;
begin
l:=ll;
i:=l;
j:=0;
repeat
inc(i);
if (s[i]=s[l]) then
inc(j);
if (s[i]=f(s[l])) then
dec(j);
until (j=-1);
exit(i);
end;
function adz(s:string;rr:longint):longint;
var
i,j,r:longint;
begin
j:=0;
i:=rr;
r:=rr;
repeat
dec(i);
if (s[i]=s[r]) then
inc(j);
if (s[i]=f(s[r])) then
dec(j);
until (j=-1);
exit(i);
end;
function check(ll,rr:longint):longint; //'()'
var
i,j,n,m,k,l,r:longint;
begin
if (s[ll]=f(s[rr]))and(rr-ll=1) then
exit(1);
l:=zdz(s,ll);
if (l-ll-1) mod 2<>0 then
sc;
if l=rr then
check:=check(ll+1,rr-1)
else
check:=check(ll,l);
//-----------------------------------------------
if check=1 then
exit(1);
r:=adz(s,rr);
if (rr-r-1) mod 2<>0 then
sc;
if r=ll then
check:=check(ll+1,rr-1)
else
check:=check(r,rr);
end;
procedure cj(s:string);
var
i:longint;
begin
if length(s) mod 2<>0 then
sc;
for i:=2 to length(s) do
if pd(i)=2 then
break
else
if max(s[i-1],s[i],1)=0 then
sc;
for i:=length(s)-1 downto 1 do
if pd(i)=1 then
break
else
if max(s[i],s[i+1],2)=1 then
sc;
end;
begin
readln(s);
cj(s);
if check(1,length(s))=1 then
writeln('YES');
end.
{2}栈:
var
s:string;
i,j,n,m,k,l:longint;
function pd(i:longint):longint;
begin
if (s[i]='[')or(s[i]='(')or(s[i]='<')or(s[i]='{') then
exit(1)
else
exit(2);
end;
procedure sc;
begin
write('NO');
halt;
end;
function f(s:string):string;
begin
if s='(' then
f:=')';
if s=')' then
f:='(';
if s='[' then
f:=']';
if s=']' then
f:='[';
if s='{' then
f:='}';
if s='}' then
f:='{';
if s='<' then
f:='>';
if s='>' then
f:='<';
end;
function hj(s:string):longint; //hua jian
begin
if (s='(')or(s=')') then
hj:=1;
if (s='[')or(s=']') then
hj:=2;
if (s='{')or(s='}') then
hj:=3;
if (s='<')or(s='>') then
hj:=4;
end;
function max(s,s1:string;n:longint):longint; //pan duan
var
min:longint;
begin
max:=hj(s);
min:=hj(s1);
if n=1 then
begin
if max>=min then
exit(1)
else
exit(0);
end;
if n=2 then
begin
if max>min then
exit(1)
else
exit(0);
end;
end;
function zdz(x:longint):longint;
var
i,j,n,m,k,l:longint;
begin
l:=x;
j:=0;
repeat
inc(l);
if l=length(s)+1 then
sc;
if (s[x]=f(s[l])) then
dec(j);
if (s[x]=s[l]) then
inc(j);
until j=-1;
exit(l);
end;
begin
readln(s);
for i:=2 to length(s)-1 do
begin
if (pd(i)=1)and(pd(i-1)=1)and(max(s[i-1],s[i],1)=0) then
sc;
if (pd(i)=2)and(pd(i+1)=2)and(max(s[i+1],s[i],1)=0) then
sc;
end;
for i:=1 to length(s) do
delete(s,zdz(i),1);
writeln('YES');
end.
- Pascal程序笔迹:弧形判断
- Pascal程序笔迹:快速幂
- Pascal程序笔迹:steve挖矿
- Pascal程序笔迹:和为零
- C++程序面试笔迹一
- C++程序面试笔迹二
- C++程序面试笔迹四
- C++程序面试笔迹五
- C++程序面试笔迹六
- 笔迹
- 笔迹
- PASCAL基础-程序体
- 我是程序猿-Pascal
- 最大正数pascal程序
- 拼数pascal程序
- 纪念品分组pascal程序
- 美元汇率pascal程序
- 零件分组pascal程序
- node常用命令汇集
- 20171202 商和余
- 深度优先搜索和广度优先搜索
- 《构建高性能web站点》笔记
- List of Well-Known TCP Port Numbers
- Pascal程序笔迹:弧形判断
- MAC IOS ssh 连接下修改环境变量
- 自我介绍
- 计蒜客 单独的数字
- 欧拉回路
- 目标检测: RCNN系列
- 1----------图(邻接矩阵)的相关算法
- 基于Opencv的人脸识别
- 排序算法之冒泡排序