1 solutions
-
0
C++ :
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n; string s[25]; bool my_judge(string); int change(char); int main() { //freopen("strs10.in","r",stdin); //freopen("strs10.out","w",stdout); cin>>n; for(int i=1;i<=n;++i) { cin>>s[i]; //my_judge(s[i]); //cout<<s[i]<<endl; if(my_judge(s[i]))cout<<"YES"<<endl; else cout<<"NO"<<endl; } } bool my_judge(string s) { int i=0,top=0,k; int a[300]; memset(a,0,sizeof(a)); a[0]=10; //cout<<s<<endl; while(i<s.size()) { k=change(s[i]); //cout<<s<<' '<<s[i]<<' '<<k<<endl; if(top==0&&k==5)return false; if(k<5) { if(k>a[top])return false; else { ++top; a[top]=k; } } if(k==5) { if(s[i]=='>') { if(a[top]==1)top--; else return false; } if(s[i]==')') { if(a[top]==2)top--; else return false; } if(s[i]==']') { if(a[top]==3) top--; else return false; } if(s[i]=='}') { if(a[top]==4)top--; else return false; } } i++; } //cout<<"y"<<endl; if(top==0)return true; } int change(char ch) { if(ch=='<')return 1; if(ch=='(')return 2; if(ch=='[')return 3; if(ch=='{')return 4; return 5; }
Pascal :
program acm21149; var s:array[1..255] of longint; i,j,k,n,top,temp:longint; st:string; f:boolean; function turn(c:char):longint; begin case c of '<': turn:=1; '(': turn:=2; '[': turn:=3; '{': turn:=4; '>': turn:=-1; ')': turn:=-2; ']': turn:=-3; '}': turn:=-4; end; end; function check:boolean; var i,j:longint; begin check:=true; for i:=1 to top do if s[i]<temp then exit(false); end; begin readln(n); for i:=1 to n do begin readln(st); top:=0; f:=true; for j:=1 to length(st) do begin temp:=turn(st[j]); if (temp>0) then if check then begin inc(top); s[top]:=temp; end else begin f:=false; break; end; if (temp<0) then if (top>0) and (s[top]+temp=0) then dec(top) else begin f:=false; break; end; end; if (top=0) and f then writeln('YES') else writeln('NO'); end; end.
- 1
Information
- ID
- 524
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By