train problem I (栈水题)
杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25276 Accepted Submission(s): 9529



3 123 312
in
in
in
out
out
out
FINISH
No.
FINISH
Hint
For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".
代码1:
- #include<cstdio>
- #include<string>
- #include<iostream>
- #define N 20
- using namespace std;
- string sc[N];
- string in,out;
- char str[N];
- int main()
- {
- int t ,i , j ,c , k ;
- while(~scanf("%d",&t))
- {
- in.clear();out.clear();
- cin>>in>>out;
- j = ;c = ; k =;
- for( i = ; i < t ; i++ )
- {
- str[c]=in[i];
- sc[k++]="in";
- while(c!=-&&str[c]==out[j])//这儿有点问题,c=0这儿有歧义,有时候栈为空,有时候不空,最好别这么写
- //也就是,str有变化的时候,第一时间修改c的值
- //这儿就是用混了,写成了c != 0,事实上,这儿c=0的时候,栈里还有一个元素
- {
- sc[k++] = "out";
- c--;j++;
- }
- c++;
- }
- if(j==t) {printf("Yes.\n");
- for( i = ;i < k ; i++)
- printf("%s\n", sc[i].c_str());} //string是不可以printf的,要用c_str函数,转为字符数组
- else printf("No.\n");
- printf("FINISH\n");//少了换行
- }
- //system("PAUSE");
- return ;
- }
代码2:
- #include<stdio.h>
- #include<string.h>
- #include<stack>
- using namespace std;
- int main()
- {
- int n, i, j, k, flag[];
- char s1[], s2[];
- stack <char> s;
- while(~scanf("%d %s%s",&n,s1,s2))
- {
- while(!s.empty()) s.pop(); //也可以不写这一句,把 stack <char> s; 就可以了
- memset(flag,-,sizeof(flag));
- j = k = ;
- for(i = ; i < n; i++)
- {
- s.push(s1[i]);
- flag[k++] = ;
- while(!s.empty() && s.top() == s2[j])
- {
- flag[k++] = ;
- s.pop();
- j++;
- }
- }
- if(j == n)
- {
- printf("Yes.\n");
- for(i = ; i < k; i++)
- {
- if(flag[i])
- printf("in\n");
- else
- printf("out\n");
- }
- }
- else
- printf("No.\n");
- printf("FINISH\n");
- }
- return ;
- }
代码3:(感谢提供代码的伟大帅气的松哥~)
- #include <cstdio>
- using namespace std;
- int main()
- {
- int n;
- char o1[], o2[];
- int stack[];
- bool ans[];
- while(~scanf("%d", &n))
- {
- scanf("%s %s", o1, o2);
- int top = , cur = , c = ;
- for(int i = ; i < n; i++)
- {
- stack[top++] = o1[i]-'';
- ans[c++] = ;
- while(top > && stack[top-] == o2[cur]-'')
- {
- top--;
- cur++;
- ans[c++] = ;
- }
- }
- if(top > ) puts("No.");
- else
- {
- puts("Yes.");
- for(int i = ; i < c; i++)
- {
- if(ans[i] == ) puts("out");
- else puts("in");
- }
- }
- puts("FINISH");
- }
- return ;
- }
train problem I (栈水题)的更多相关文章
- HDU 5832 A water problem(某水题)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】
CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...
- Train Problem I(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Hdu 1022 Train Problem I 栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Train Problem I (HDU 100题纪念)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 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 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem C. Contest 水题
Problem C. Contest 题目连接: http://codeforces.com/gym/100714 Description The second round of the annual ...
- Gym 100646 Problem E: Su-Su-Sudoku 水题
Problem E: Su-Su-Sudoku/center> 题目连接: http://codeforces.com/gym/100646/attachments Description By ...
随机推荐
- Xamarin Android Fragment的两种加载方式
android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...
- MySQL在字段中使用select子查询
前几天看别人的代码中看到在字段中使用select子查询的方法,第一次见这种写法,然后研究了一下,记录下来 大概的形式是这样的: select a .*,(select b.another_field ...
- PHP-无限级分类
给定省市地区数组如下: $area = array( array('id'=>1,'name'=>'安徽','parent'=>'0'), ...
- 2.Nginx日常维护技巧
Nginx日常维护技巧 Nginx配置正确性检查 nginx提供了配置文件调试功能,可以快速定义配置文件存在的问题.执行如下命令检测配置文件的正确性: [root@localhost 桌面]# whi ...
- Kotlin——最详细的抽象类(abstract)、内部类(嵌套类)详解
如果您对Kotlin很有兴趣,或者很想学好这门语言,可以关注我的掘金,或者进入我的QQ群大家一起学习.进步. 欢迎各位大佬进群共同研究.探索QQ群号:497071402 进入正题 在前面几个章节中,详 ...
- Apache Avro# 1.8.2 Specification (Avro 1.8.2规范)一
h4 { text-indent: 0.71cm; margin-top: 0.49cm; margin-bottom: 0.51cm; direction: ltr; color: #000000; ...
- Xampp配置本地域名及常见错误解决
本地域名配置 1.计算机-->C盘-->Windows-->System32-->drivers-->etc-->hosts 127.0.0.1 loc ...
- 基于zepto的移动端日期和时间选择控件
前段时间给大家分享过一个基于jQuery Mobile的移动端日期时间拾取器,大家反应其由于加载过大的插件导致影响调用速度.那么今天我把从网络上搜集到的两个适合移动端应用的日期和时间选择插件分享给大家 ...
- powerdesigner的使用
前言 做过建模和设计的人都知道,powerdesigner是个强大实用的工具:采用模型驱动方法,将业务与IT结合起来,可帮助部署有效的企业体系架构,并为研发生命周期管理提供强大的分析与设计技术.本文档 ...
- ${param.name}和${name}的区别
${param.name} == request.getParam("name") ${name} == request.getAttribute("name" ...