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
 
本题的大意是,有一个火车站,只有一个入口一个出口...随便输入一组进站火车编号(不超过10),一组出战火车编号。要求判断能否按照出站编号出站,如果可以接把顺序输出。如果
不能输出No.。
开始拿到本题,感觉无从下手,想了两人,最后写出一个程序能够通过给出的测试数据。但是WA...后来上网查找资料,大神们都说很简单,只是一个栈的简单应用,(因为没有学过C++)顿时感觉自己懂的东西太少了...查阅了相关资料...
原来栈是定义在头文件stack里面的一个容器类型,想要定义一个栈一般有

 stack<char> s1;
stack<int> s2;
stack<string> s3;

毋庸置疑,和我们定义一个整形很像,但是尖括号里面是栈类型的说明....栈的基本操作有

 入栈
s.push(x);
出栈
s.pop();//这里括号里面没有元素,弹出的是栈顶的元素
判断栈是否为空
s.empty();//如果为空,返回true值
栈顶的元素
s.top();
访问栈元素的个数
s.size();
等等.....

掌握了这些知识后,再根据网上大神写的代码,自己再敲了一个代码...贴出如下

 /********************************************
杭电acm 1022 已AC
*****************************************/
#include "iostream"
#include "string"
#include "stack"
using namespace std;
#define Max 10
int main(void)
{
int len;
char inarr[Max],outarr[Max];
int i=,j=;
int flag[]={};
while(scanf("%d %s %s",&len,inarr,outarr)!=EOF)
{
stack<char> temp;
i=;j=;
for(i;i<len;)
{
if(temp.empty())
{
temp.push(inarr[i]);
flag[i+j]=;
i++;
}
if(!temp.empty()&&temp.top()!=outarr[j])
{
temp.push(inarr[i]);
flag[i+j]=;
i++;
}
//这里要注意,不能使用if,开始使用if,一直不能AC,这里使用while是要将所有符合规则的
//元素都弹出栈...
while(!temp.empty()&&temp.top()==outarr[j])
{
temp.pop();
flag[i+j]=;
j++;
}
}
if(temp.empty())
{
cout<<"Yes."<<endl;
for(int m=;m<*len;m++)
{
if(flag[m]==)
cout<<"in"<<endl;
else cout<<"out"<<endl;
}
cout<<"FINISH"<<endl;
}
else cout<<"No."<<endl<<"FINISH"<<endl; }
return ;
}

特别要注意35行的代码...

继续努力.....

杭电acm 1022题的更多相关文章

  1. 杭电acm 1076题

    水题,一个求闰年的题目,复习一下闰年的求法.... 1,如果能被4整除但不能被100整除的是闰年 2,能被400整除的是闰年 题目大意是:给定一个开始年份T以及一个正数N,要求求出从T开始,到了哪一年 ...

  2. 杭电acm 1037题

    本题应该是迄今为止最为简单的一道题,只有一组输入,输出也简单.... /****************************************** 杭电acm 1037题 已AC ***** ...

  3. 杭电acm 1038题

    本题比较简单,但是需要掌握几个小技巧,先上代码 /************************************* 杭电ACM 1038题,已AC ********************* ...

  4. 杭电acm 1049题

    一道水题..... 大意是一条1inch的虫子在一个n inch的盒子的底部,有足够的能够每一分钟往上爬u inch,但是需要休息一分钟,这期间会往下掉d inch,虫子爬到盒子口即认为结束.要求计算 ...

  5. 杭电acm 1033题

    Problem Description For products that are wrapped in small packings it is necessary that the sheet o ...

  6. 杭电ACM刷题(1):1002,A + B Problem II 标签: acmc语言 2017-05-07 15:35 139人阅读 评

    最近忙于考试复习,没有多少可供自己安排的时间,所以我利用复习之余的空闲时间去刷刷杭电acm的题目,也当对自己编程能力的锻炼吧. Problem Description I have a very si ...

  7. 杭电acm刷题顺序

    最近兴趣来了,闲暇之余,回顾大学期间刷过的杭电acm那些入门级别的题,以此巩固基础知识! 以下参考刷题顺序,避免入坑 原文传送门:https://blog.csdn.net/liuqiyao_01/a ...

  8. 杭电acm 1015题

    马上要找工作了,锻炼下自己的写程序能力,不多说,上代码 /********************杭电acm 1015 已AC 在这个程序里,使用穷举法来实现,但是输出顺序需要安装字典的最大 来输出 ...

  9. 杭电acm 1040题

    本题是一个非常简单的升序排序题目,但那时在做的时候把题目看错了,导致花费了大量的时间来检查为什么WA,最后发现题目看错了..... /********************************* ...

随机推荐

  1. HTML相对路径与绝对路径

    在网页制作的过程中,少不了跟路径打交道,比如,包含一个文件,插入一个图片等,与路径都有关系,如果使用了错误的文件路径,就会导致引用失效(无法浏览链接文件,或无法显示插入的图片等).初学者可能会感到困惑 ...

  2. shiro2

    mapper接口:根据用户id查询用户权限的菜单 service接口:根据用户id查询用户权限的菜单 获取用户权限范围的url 思路: 在用户认证时,认证通过,根据用户id从数据库获取用户权限范围的u ...

  3. poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】

    题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...

  4. ruanjiangongcheng1

    软体工程的方法有很多方面的意义.包括专案管理,分析,设计,程序的编写,测试和质量控制. 软体设计方法可以区别为重量级的方法和轻量级的方法.重量级的方法中产生大量的正式文档. 著名的重量级开发方法包括I ...

  5. python第四篇:linux命令行总结 + 自动备份Python程序

    由于最近需要学习Python爬虫相关的知识,所以就先从Python基础.Linux基础开始进行了学习,下面主要是总结了常见的Linux的命令行.最后为了巩固学到的东西,尝试写了个自动备份的Python ...

  6. Codeforces 571B Minimization:dp + 贪心【前后相消】

    题目链接:http://codeforces.com/problemset/problem/571/B 题意: 给你一个长度为n的数列a[i]. 现在你可以随意改变数字的位置,问你 ∑| a[i] - ...

  7. elasticsearch的store属性跟_source字段——如果你的文档长度很长,存储了_source,从_source中获取field的代价很大,你可以显式的将某些field的store属性设置为yes,否则设置为no

    转自:http://kangrui.iteye.com/blog/2262860 众所周知_source字段存储的是索引的原始内容,那store属性的设置是为何呢?es为什么要把store的默认取值设 ...

  8. Linux下system函数

    http://www.jb51.net/article/40517.htm   浅析如何在c语言中调用Linux脚本 http://blog.csdn.net/koches/article/detai ...

  9. Selenium-使用firepath识别元素

    利用firepath进行元素识别提前已经安装好firebug和firepath 比如,打开http://www.baidu.com 1.按下F12 2.点击如图的位置 3.选择元素,可以定位出元素的属 ...

  10. 关于c++中局部变量和全局变量的存储位置及内存回收机制

    局部变量,参数变量存放在栈中,当离开作用范围后,分配的内存在作用范围外会被系统自动回收. new出来的内存空间存放在堆中,不受作用域管理,不会被系统自动回收,只有在使用delete删除或者整个程序结束 ...