Train Problem I(栈)
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25773 Accepted Submission(s): 9729



- #include<stdio.h>
- #include<string.h>
- #include<stack>
- using namespace std;
- char in[],out[],m[][];
- int t;
- void display(char *s){
- strcpy(m[t++],s);
- }
- int main(){
- int n;
- while(~scanf("%d",&n)){memset(m,,sizeof(m));
- memset(in,,sizeof(in));
- memset(out,,sizeof(out));
- t=;scanf("%s%s",in,out);
- stack<char>train;
- for(int j=,i=;i<n;){
- if(!train.empty()&&train.top()==out[i])display("out"),i++,train.pop();
- else if(in[j])train.push(in[j++]),display("in");
- else break;
- }//printf("%d\n",train.size());
- //while(!train.empty())printf("%c",train.top()),train.pop();
- display("FINISH");
- if(train.empty()){puts("Yes.");
- for(int i=;i<t;++i)printf("%s\n",m[i]);}
- else puts("No."),puts("FINISH");
- }
- return ;
- }
另外,自己写了几个关于栈的括号配对问题,贴下:
代码:
- #include<stdio.h>
- char m[];
- int top;
- bool pop(){
- top--;
- if(top<)return false;
- else return true;
- }
- void push(char s){
- top++;
- m[top]=s;
- }
- int main(){
- char x[];
- int T;
- scanf("%d",&T);
- while(T--){top=;
- scanf("%s",x);
- for(int i=;x[i];i++){
- if(x[i]=='('||x[i]=='[')push(x[i]);
- else if(x[i]==')'&&m[top]=='('||x[i]==']'&&m[top]=='['){if(!pop())break;}
- else push(x[i]);
- }//printf("%d",top);
- //while(top)printf("%c",m[top--]);
- if(top==)puts("Yes");
- else puts("No");
- }
- return ;
- }
- #include<stdio.h>
- #include<stack>
- using namespace std;
- char s[];
- int main(){
- int T,temp;
- scanf("%d",&T);
- while(T--){temp=;
- stack<char>m;
- scanf("%s",s);
- for(int i=;s[i];i++){if(m.empty()&&(s[i]==')'||s[i]==']')){
- temp=;
- puts("No");
- break;
- }
- if(s[i]=='('||s[i]=='[')m.push(s[i]);
- else if(s[i]==')'&&m.top()=='('||s[i]==']'&&m.top()=='[')m.pop();
- else m.push(s[i]);
- }
- if(m.empty()&&temp)puts("Yes");
- else if(temp)puts("No");
- }
- return ;}
Train Problem I(栈)的更多相关文章
- train problem I (栈水题)
杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...
- Hdu 1022 Train Problem I 栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- Train Problem(栈的应用)
Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...
- HDU1022 Train Problem I 栈的模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 栈的模拟,题目大意是已知元素次序, 判断出栈次序是否合理. 需要考虑到各种情况, 分类处理. 常 ...
- Train Problem I--hdu1022(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1022.Train Problem I【栈的应用】【8月19】
Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...
- HDU - 1022 Train Problem I STL 压栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1022 Train Problem I(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 判断一个key 是否在map中存在
public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-gener ...
- web前端之 CSS引入第三方插件
引入第三方图标插件 - fontawesome 官网地址:http://fontawesome.io/ 1.下载图标插件包 下载地址:https://codeload.github.com/FortA ...
- hdu 5063 Operation the Sequence(Bestcoder Round #13)
Operation the Sequence Time Limi ...
- 7. 稀疏表示之OMP,SOMP算法及openCV实现
一.前言 稀疏表示是自上世纪90年代开始,从人眼的视觉感受野获得启示,逐渐被人们所研究.现在已经发展为一种重要的信息表示方法.所谓稀疏表示是指,一个信号在过完备字典中,可以由少数个原子线性表达, 其数 ...
- python re 正则
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- ASP.NET中多个相同name的控件在后台正确取值
有兽, 页面上可能有多个相同name的Html表单控件, 一般在后台使用Request.Form[“name”]取值,并用‘,’分隔. 但是当值中包含逗号时, 取值就会出现异常, ...
- 如何更改 Mac OS X 系统默认用户名
说到 Mac 用户名估计有许多人都不知道在哪个地方修改,其实说简单也简单说麻烦也麻烦看你自己的需求.好比如果你只要用户名的登录更改,那是就非常简单的事了.下面这里就给大家介绍mac osx系统如何更改 ...
- 创建对象时引用的关键字,assign,copy,retain
创建对象时引用的关键字:assign: 简单赋值,不更改索引计数(强引用)copy: 建立一个索引计数为1的对象,然后释放旧对象retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索 ...
- 读jquery.cookie.js源码学到的几个技巧
一.兼容AMD.CommonJS和普通JS的写法 (function (factory) { if (typeof define === 'function' && define.am ...
- JQuery中阻止事件冒泡的两种方式及其区别
JQuery 提供了两种方式来阻止事件冒泡. 方式一:event.stopPropagation(); $("#div1").mousedown(function(event){ ...