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. linux下搭建java开发环境

    1 下载jdk包 这里下载.gz格式的,通过ftp上传到服务器 2 解压到指定目录,如/usr/java/ tar -xvf XXX.tar.gz 解压后会在/usr/java下生成一个目录,如jdk ...

  2. UIView的概念与使用

    什么是UIView UIView可以称之为控件/视图 屏幕上所有能看到的东西都是UIView 按钮(UIButton).文本(UILabel)都是控件 控件都有一些共同的属性 -- 尺寸 -- 位置 ...

  3. 算法(Algorithms)第4版 练习 1.3.4

    主要思路: 遇到左括号则一直压栈,遇到右括号时则从栈中弹出一个元素. 如果此时栈为空,则返回false. 如果这个元素与右括号不匹配,则返回false. 重复此过程,最后判断栈是否为空,若为空则返回t ...

  4. Sqlte 知识点记录

    1.表存在 select count(*) from sqlite_master where type='table' and name='MyTable'; sql),path ))"; ...

  5. WEB安全之Token浅谈

    Token一般用在两个地方——防止表单重复提交.anti csrf攻击(跨站点请求伪造). 两者在原理上都是通过session token来实现的.当客户端请求页面时,服务器会生成一个随机数Token ...

  6. Python3之hashlib模块

    Python3之hashlib   简介: 用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法.在python3中已 ...

  7. hdu 2018 母牛的故事(简单dp)

    母牛的故事 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. css 多行文本以...代替

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. java 任务调度实现的总结

    Timer Timer的核心是Timer和TimerTask,Timer负责设定TimerTask的起始与间隔执行时间,使用者需要建立一个timeTask的继承类,实现run方法,然后将其交给Time ...

  10. es6的foreach循环遍历

    forEach forEach是Array新方法中最基本的一个,就是遍历,循环.例如下面这个例子: 结果: 这段代码相当于: for (var k = 0, length = array.length ...