题目:

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
 
 
 

Hint

For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".

 
 题意:
  可以等价为下图中左侧序列,题目要求经过中间栈,是否得到右侧的目标序列。并输出火车进出站的次序。
 

图一

图二

图三

正解:
 #include <iostream>
#include <stack>
#include <string>
#include <bits/stdc++.h>
using namespace std; #define IN 1
#define OUT 0
string a,b;
#define MAX_NUM 100100 int sep[MAX_NUM]; int main(int argc, char const *argv[])
{
int n;
while(cin >> n >> a >> b){
stack<char> s;
int i = , j = , k = ;
s.push(a[]);
sep[k++] = IN;
while(i < n && j < n){
if(s.size() != && s.top() == b[j]){
j++;
s.pop();
sep[k++] = OUT;
}else{
s.push(a[++i]);
sep[k++] = IN;
}
}
if(i == n){//如果i==n表示栈顶元素不等于序列2当前元素,且序列1中元素都已经入过栈,判断不能得到序列2一样的答案。
cout << "No." << endl;
}else{
cout << "Yes." << endl;
for (int i = ; i < k; ++i)
{
if(sep[i]){
cout << "in" << endl;
}
else{
cout << "out" << endl;
}
}
}
cout << "FINISH" << endl;
}
return ;
}

HDU_1022的更多相关文章

随机推荐

  1. webpack(3)--Output

    Output output配置如何输出最终想要的代码,output是一个object里面包含一系列配置. 1. filename output.filename配置输出文件的名称,为string类型, ...

  2. 解决Sybase PowerDesigner 数据库设计中 Name 自动填充Code

    在使用 Sybase PowerDesigner 进行数据库设计时,为了理清思路,需要将name改为中文名称,但是这个软件会自动将name填 充为code,可以通过如下配置修改: 选择tools-&g ...

  3. 经典算法 Morris遍历

    内容: 1.什么是morris遍历 2.morris遍历规则与过程 3.先序及中序 4.后序 5.morris遍历时间复杂度分析 1.什么是morris遍历 关于二叉树先序.中序.后序遍历的递归和非递 ...

  4. JQ-用户注册用到的图形验证码,短信验证码点击事件,切换active类

    // 点击切换图形验证码 页面加载完后执行,类似window.onload $(function () { var imgCaptcha = $(".img-captcha"); ...

  5. PY3 周总结 第一周

    1. Python的三大特点是什么? 答:解释型.动态类型(运行期间才做数据类型检查).强类型定义(一个变量只能存储一种类型的数据).[See More] 2. 应该使用Python2 还是 Pyth ...

  6. elt区间分布

    select DATE_FORMAT(CURDATE(),'%Y%m%d') DateId,elt(interval(curnum,0, 10000,20000,30000,40000,50000, ...

  7. activemq的学习

    https://blog.csdn.net/csdn_kenneth/article/category/7352171/1

  8. HtmlRowCreated关于e.Row.Cells[0]的获取和设置

    获取采用:  cmd2.Parameters.AddWithValue("@xh", e.GetValue("学号").ToString().Trim()); ...

  9. 从Chrome 69.0 版本起,Flash权限受到进一步限制,默认仅在当前浏览器会话有效。

    # 69.0 之后的版本 ## 从Chrome 69.0 版本起,Flash权限受到进一步限制,默认仅在当前浏览器会话有效.关闭Enable Ephemeral Flash Permissions , ...

  10. ant 注意

    nt文件在部署时,如果控制台出现乱码则需要调整语言. 高版本eclipse在jdk高版本中已经植入了ant的部署.因此不需要单独配置ant.jar. 如果版本低,可下载ant插件,或者下载ant的工具 ...