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. Data Structure Binary Tree: Morris traversal for Preorder

    http://www.geeksforgeeks.org/morris-traversal-for-preorder/ #include <iostream> #include <v ...

  3. python链表的实现

    根据Problem Solving with Algorithms and Data Structures using Python 一书用python实现链表 书籍在线网址http://intera ...

  4. 总结:iview(基于vue.js的开源ui组件)学习的一些坑

    1.要改变组件的样式 找到这个组件的class名,然后覆盖样式. 举例:修改select框,显示圆角.只需给找到类名并写样 .ivu-select-selection{ border-radius:1 ...

  5. Linux 上通过rpm安装mysql

    安装mysql之前要remove掉系统自带的mysql: rpm -qa | grep "MySQL*"    和rpm -qa | grep mysql  要确保卸载干净 rpm ...

  6. 【JavaScript学习整理】js基础

    HTML,CSS属于标记语言, JavaScript是基于客户端的脚本语言. 变量: 语法  var 变量名 = value var是系统内部关键字,用来声明变量 变量名规则:  1.不能以数字开头  ...

  7. Java的TCP网络编程

    服务端代码: package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  8. Angular-ui-router路由,View管理

    ui-router的工作原理非常类似于Angular的路由控制,他只关注状态. Angular模板 最简单的模板,例如main.html: <body data-ng-app="myA ...

  9. 英语发音规则---th

    英语发音规则---th 一.总结 一句话总结: th发/ð/音的情况:a.在th后以字母-er结尾的单词中:b.在代词.冠词.介词.连词或副词中的字母组合th th发/θ/音的情况:a.在数词(包括基 ...

  10. SQL2005 2008配置错误,无法识别的配置节 system.serviceModel machine.config配置文件有问题

    当装上2008的时候,你以前的程序突然报出你的machine.config配置文件有问题,比如 “/” 应用程序中的服务器错误. 配置错误 说明 : 在处理向该请求提供服务所需的配置文件时出错.请检查 ...