标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25773 Accepted Submission(s): 9729
1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 using namespace std; 5 char in[20],out[10],m[30][5]; 6 int t; 7 void display(char *s){ 8 strcpy(m[t++],s); 9 } 10 int main(){ 11 int n; 12 while(~scanf("%d",&n)){memset(m,0,sizeof(m)); 13 memset(in,0,sizeof(in)); 14 memset(out,0,sizeof(out)); 15 t=0;scanf("%s%s",in,out); 16 stack<char>train; 17 for(int j=0,i=0;i<n;){ 18 if(!train.empty()&&train.top()==out[i])display("out"),i++,train.pop(); 19 else if(in[j])train.push(in[j++]),display("in"); 20 else break; 21 }//printf("%d\n",train.size()); 22 //while(!train.empty())printf("%c",train.top()),train.pop(); 23 display("FINISH"); 24 if(train.empty()){puts("Yes."); 25 for(int i=0;i<t;++i)printf("%s\n",m[i]);} 26 else puts("No."),puts("FINISH"); 27 } 28 return 0; 29 }
另外,自己写了几个关于栈的括号配对问题,贴下:
代码:
1 #include<stdio.h> 2 char m[10010]; 3 int top; 4 bool pop(){ 5 top--; 6 if(top<0)return false; 7 else return true; 8 } 9 void push(char s){ 10 top++; 11 m[top]=s; 12 } 13 int main(){ 14 char x[10010]; 15 int T; 16 scanf("%d",&T); 17 while(T--){top=0; 18 scanf("%s",x); 19 for(int i=0;x[i];i++){ 20 if(x[i]==‘(‘||x[i]==‘[‘)push(x[i]); 21 else if(x[i]==‘)‘&&m[top]==‘(‘||x[i]==‘]‘&&m[top]==‘[‘){if(!pop())break;} 22 else push(x[i]); 23 }//printf("%d",top); 24 //while(top)printf("%c",m[top--]); 25 if(top==0)puts("Yes"); 26 else puts("No"); 27 } 28 return 0; 29 }
1 #include<stdio.h> 2 #include<stack> 3 using namespace std; 4 char s[10010]; 5 int main(){ 6 int T,temp; 7 scanf("%d",&T); 8 while(T--){temp=1; 9 stack<char>m; 10 scanf("%s",s); 11 for(int i=0;s[i];i++){if(m.empty()&&(s[i]==‘)‘||s[i]==‘]‘)){ 12 temp=0; 13 puts("No"); 14 break; 15 } 16 if(s[i]==‘(‘||s[i]==‘[‘)m.push(s[i]); 17 else if(s[i]==‘)‘&&m.top()==‘(‘||s[i]==‘]‘&&m.top()==‘[‘)m.pop(); 18 else m.push(s[i]); 19 } 20 if(m.empty()&&temp)puts("Yes"); 21 else if(temp)puts("No"); 22 } 23 return 0;}
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4679652.html