杭电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

Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
 
Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
 
Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
 
Sample Input
3 123 321
3 123 312
 
Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

Hint

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.".

 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for you:  1026 1023 1004 1032 1010 
 
 
这个题要是手写栈的话一定要注意范围各个参数的范围
 
题意: 火车进站,出站能否按所给顺序出站  标准栈。
 

  代码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 (栈水题)的更多相关文章

  1. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  2. CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】

    CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...

  3. Train Problem I(栈)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. Hdu 1022 Train Problem I 栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. Train Problem I (HDU 100题纪念)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  6. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  7. Train Problem(栈的应用)

    Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...

  8. 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 ...

  9. Gym 100646 Problem E: Su-Su-Sudoku 水题

    Problem E: Su-Su-Sudoku/center> 题目连接: http://codeforces.com/gym/100646/attachments Description By ...

随机推荐

  1. iOS中self与_的区别

    同时我们发现在我们访问我们声明的变量时,会有self. 和 以"_"开头的访问方式,那么这两种方式到底有什么样的区别呢? 我们来一起看一下: @property (retain, ...

  2. mouseout、mouseover和mouseleave、mouseenter区别

    今天在使用鼠标事件时,用错了mouseout,于是做个测试总结. 结论: mouseenter:当鼠标移入某元素时触发. mouseleave:当鼠标移出某元素时触发. mouseover:当鼠标移入 ...

  3. Vuex- Action的 { commit }

    Vuex 中 使用 Action 处理异步请求时,常规写法如下: getMenuAction:(context) =>{ context.commit('SET_MENU_LIST',['承保2 ...

  4. Python中将函数作为另一个函数的参数传入并调用

    在Python中,函数本身也是对象,所以可以将函数作为参数传入另一函数并进行调用 在旧版本中,可以使用apply(function, *args, **kwargs)进行调用,但是在新版本中已经移除, ...

  5. jQuery DOM 元素方法 (十)

    函数 描述 .get() 获得由选择器指定的 DOM 元素. .index() 返回指定元素相对于其他指定元素的 index 位置. .size() 返回被 jQuery 选择器匹配的元素的数量. . ...

  6. JavaMail开发教程01开山篇

    序 其实想写JavaMail这一系列的博客已经有一个月之久了,缘起是某次乱逛传智播客官网浏览到相关的视频教程,想起大学时代学过的计算机网络提到邮件相关的协议,但遗憾的是到目前为止还没有接触计算机网络编 ...

  7. [Micropython]发光二极管制作炫彩跑马灯

       先甩锅 做完后才发现最后一个灯坏了,就坏了一个灯也不好意思去找淘宝店家,大家视频凑合着看把.不过并不影响实验效果.因为这个发光二极管白天不是很明显 晚上炫彩效果就能出来了.本次实验用的是8个灯珠 ...

  8. angular4.0 安装最新版本的nodejs、npm、@angular/cli的方法

    在使用ng项目的ui框架时,比如ng-zorro.angular Material,需要安装最新版本的@angular/cli: 配置ng-zorro框架 ng-zorro官网:https://ng. ...

  9. vue2.0 正确理解Vue.nextTick()的用途

    什么是Vue.nextTick() 官方文档解释如下: 在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 获取更新后的DOM,言外之意就是DOM更新 ...

  10. iOS学习——获取iOS设备的各种信息

    不管是在Android开发还是iOS开发过程中,有时候我们需要经常根据设备的一些状态或信息进行不同的设置和性能配置,例如横竖屏切换时,电池电量低时,内存不够时,网络切换时等等,我们在这时候需要进行一些 ...