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. Oracle分页总汇

    Oracle分页总汇 select * from (select a.*,rownum row_num from (select * from mytable t order by t.id desc ...

  2. shell一些方法

    字符串截取转自原文地址:http://www.jb51.net/article/56563.htm 一:字符串截取 有var变量: var=http://www.aaa.com/123.htm 1. ...

  3. php 实现简单抽奖

    首先有一组数据,里面有中奖的物品和概率 $base_data = [ ['name'=>'特等奖','num'=>1], ['name'=>'一等奖','num'=>5], [ ...

  4. [原创]Scala学习:流程控制,异常处理

    1.流程控制 1)do..while def doWhile(){ var line="" do{ line = readLine() println("readline ...

  5. Spring中ApplicationContext和beanfactory区别

    BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...

  6. 剑指offer之 替换空格

    package Problem4; public class ReplaceBank { /* * 题目描述: 请实现一个函数,将字符串的每个空格替换为"%20". * 例如输入& ...

  7. HDU5371 Hotaru's problem

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. Spark- 计算每个学科最受欢迎的老师

    日志类型 测试数据 http://bigdata.myit.com/zhangsan http://bigdata.myit.com/zhangsan http://bigdata.myit.com/ ...

  9. 纯CSS3跳动焦点广告轮播特效

    1. [代码] 纯CSS3跳动焦点广告轮播特效 <!--  Author: Developed by Caleb Jacob Author Website: http://iamceege.co ...

  10. Cocos2d-x中常用宏的作用

    1. CC_SYNTHESIZE(int, nTest, Test); 相当于: protected: int nTest; public: virtual nTest getTest(void) c ...