Train Problem I

时间限制:3000 ms  |  内存限制:65535 KB
难度:2
描述
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.
输入
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.
输出
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.
样例输入
  1. 3 123 321
  2. 3 123 312
样例输出
  1. Yes.
  2. in
  3. in
  4. in
  5. out
  6. out
  7. out
  8. FINISH
  9. No.
  10. FINISH
    [题意]:给出两个长n的串,入栈和出栈顺序.判断是否可以.
    [分析]:

比较坑的一点就是 str2的长度可能大于str1. 于是str1[i] 压入栈标记为1 如果栈顶==str2[j] 就出栈 j++ 标记为0

j==n yes,否则 no

  1. [代码]:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=;
  4. int main()
  5. {
  6. int n,vis[N];
  7. char s1[N],s2[N];
  8. stack<char> st;
  9. while(cin>>n>>s1>>s2)
  10. {
  11. while(!st.empty()) st.pop();//清空栈
  12. memset(vis,,sizeof(vis));
  13. int j=,k=; //k记录路径数,vis标记路径
  14. for(int i=;i<n;i++)
  15. {
  16. st.push(s1[i]);
  17. vis[k++]=;
  18. while(!st.empty() && st.top()==s2[j])
  19. {
  20. st.pop();
  21. vis[k++]=;
  22. j++;
  23. }
  24. }
  25. if(j==n)
  26. {
  27. puts("Yes.");
  28. for(int i=;i<k;i++) //k记录路径数,vis标记路径
  29. puts(vis[i]?"in":"out");
  30. puts("FINISH");
  31. }
  32. else
  33. printf("No.\nFINISH\n");
  34. }
  35. }
  36. /*
  37. 3
  38. 123 213
  39. Yes.
  40. in
  41. in
  42. out
  43. out
  44. in
  45. out
  46. FINISH
  47. */

HDU 1022 Train Problem I[给出两个长n的串,入栈和出栈顺序,判断入栈顺序是否可以匹配出栈顺序]的更多相关文章

  1. HDU 1022 Train Problem I(栈的操作规则)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...

  2. HDU 1022 Train Problem I

    A - Train Problem I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. hdu 1022 Train Problem I【模拟出入栈】

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. Hdu 1022 Train Problem I 栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU 1022.Train Problem I【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  6. HDU - 1022 Train Problem I STL 压栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. hdu 1022:Train Problem I(数据结构,栈,递归,dfs)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. hdu 1022 Train Problem I(栈的应用+STL)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu 1022 Train Problem

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. 最短路径:HDU2006-一个人的旅行(多个起点,多个终点)

    题目: 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 3、python中的字符串

    一.前言 字符串是python中重要的数据类型.字符串就是一段文本,在python中用引号来标示. 二.字符串分类 字符串根据使用场景不同,一共分成3类: (1)单引号.双引号创建的单行字符串: 在单 ...

  3. 《鸟哥的Linux私房菜》学习笔记(5)——权限管理

    一.权限的基本概念                                                   权限:访问计算机资源或服务的访问能力. Linux中,每一个资源或者服务的权限, ...

  4. java十分钟速懂知识点——引用

    一.由健忘症引起的问题 今天闲来没事在日志中瞟见了个OutOfMemoryError错误,不由得想到前一段时间看到一篇面经里问到Java中是否有内存泄露,这个很久以前是留意过的,大体记得内存溢出和内存 ...

  5. iview框架 两侧弹框 出现第二层弹框 一闪而过的问题

    分析原因:寡人怀疑可能是,两层弹出框 采用的是一个开关值,发生了覆盖 解决方式 是在第二层弹框外套层计时器 源代码如下: 修改后为:

  6. Python框架之Django学习笔记(十二)

    Django站点管理 十一转眼结束,说好的充电没能顺利开展,反而悠闲的看了电视剧以及去影院看了新上映的<心花路放>.<亲爱的>以及<黄金时代>,说好的劳逸结合现在回 ...

  7. C 语言 习题 1-9

    练习1-9 编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替. #include <stdio.h> int main(int argc, char const *ar ...

  8. uncompyle2反编译python的.py文件

    前几天学用github,一不小心把a.py文件给删除了,由于1天没有提交,也无法找回.突然发现同a.py文件生成的编译文件a.pyc还在,逐去搜索一番反编译的方法. 查询得知python比较好的工具u ...

  9. [oldboy-django][2深入django]老师管理 -- form表单如何生成多选框标签,多选框的默认值显示,以及多选框数据插入到数据库,多选框数据更改到数据库

    1 form表单如何生成多选框(包含了多选框可选择内容) - Form设置班级输入框为 select多选 - 多选 class TeacherForm(Form): name = fields.Cha ...

  10. sizeof 数组名字,数组指针

    int *a = new int[15]; sizeof(a)//在64位机器上,8 sizeof(a)/sizeof(int) //2 ----- int a[15]; sizeof(a)//15* ...