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. pygame躲敌人的游戏

    #first.py# coding=utf- import pygame from pygame.locals import * from sys import exit from util impo ...

  2. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. 20165101 学习基础和C语言基础调查

    学习基础和C语言基础调查 技能学习心得 看了15级学长学姐丰富的技能之后,我感到很惭愧.我的课外技能可以说是很糟糕.唱歌的话,小时候还可以用假声唱一下,变声之后就是高音上不去,低音下不来.体育更是差劲 ...

  4. SDUT 1048 Digital Roots

    Digital Roots Time Limit: 1000ms   Memory limit: 65536K 题目描述 The digital root of a positive integer ...

  5. css 网站素装 追忆过去

    素装代码,以表哀悼等.以下为全站CSS代码. html { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: ...

  6. 分享知识-快乐自己:Liunx 安装MySQL

    第一步: 1):下载mysql安装包:这里选择下载版本 5.6.33,通用版,linux下64位 http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql- ...

  7. javascript通用参数判断

    //判断value是小于等于max的数字function isNumberMax(value, max){    if(!isNumber(value) || !isNumber(max)){     ...

  8. sqlserver 新建只读权限用户

    1,新建只能访问某一个表的只读用户. --添加只允许访问指定表的用户:execsp_addlogin'用户名','密码','默认数据库名' --添加到数据库execsp_grantdbaccess'用 ...

  9. 一种解决 MacBook 里的 App Store 无法登录的问题

    刚刚买回来的 2018 款带有 touchbar 的 MacBook Pro 15 inc 在用 App Store 安装 app 时一直无法登录成功(网络链接都是好的),导致软件都无法更新,折腾了挺 ...

  10. POJ2406Power Strings (最小循环节)(KMP||后缀数组)

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...