题目:

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. a标签解析url

    var url = 'http://127.0.0.1:8080/index.jsp?username=admin#name'; var aLink = document.createElement( ...

  2. flask高阶

    内容: 1.进程线程复习 2.flask多线程的问题 3.线程隔离 4.LocalStack 5.flask上下文整理 6.多app应用 1.进程线程复习 (1)进程 进程是一个具有一定独立功能的程序 ...

  3. C# 日志记录工具类--LogHelper.cs测试

    C# 日志记录工具类:(适用于不想使用log4j等第三方的Log工具的时候,希望自己写个简单类实现)LogHelper.cs内容如下: using System; using System.Diagn ...

  4. 关于tcp的三次握手与四次挥手,以及粘包

    tcp三次握手: TCP是因特网中的传输层协议,使用三次握手协议建立连接.当主动方发出SYN连接请求后,等待对方回答SYN+ACK[1],并最终对对方的 SYN 执行 ACK 确认.这种建立连接的方法 ...

  5. RouterOS 5.16软路由安装图解教程

    说明:RouterOS是一种路由器操作系统,它可以安装到普通的个人电脑上面,替代硬件路由器 RouterOS版本:RouterOS 5.16 硬件要求: 1.支持多核CPU 2.内存最大支持到2G 3 ...

  6. Eclipse配置Tomcat,访问404错误

    我从官网上面下载的tomcat6,直接启动发现正常使用,但是在Eclipse绑定后启动,访问localhost:8080,本来应该是tomcat的主页,但是却报了404错误. 百度搜索了一下,原来是t ...

  7. psc格式的文件是什么

    psc格式的文件是什么 是navicat 这个工具导出的数据文件 可以使用备份还原功能  提取sql

  8. Xshell使用

    xshell更新之后窗口标题没了,多个窗口之后没法切换. 使用ctrl+shift+t恢复 ¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥

  9. 1. java获取本周日-本周六的时间

    Calendar calendar = Calendar.getInstance(); String[] arrDate = new String[5]; String[] arrWeek = new ...

  10. 9 个Java 异常处理的规则

    在 Java 中,异常处理是个很麻烦的事情.初学者觉得它很难理解,甚至是经验丰富的开发者也要花费很长时间决定异常是要处理掉和抛出. 所以很多开发团队约定一些原则处理异常.如果你是一个团队的新成员,你可 ...