HDU1022 Train Problem I 栈的模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042
栈的模拟,题目大意是已知元素次序, 判断出栈次序是否合理。 需要考虑到各种情况, 分类处理。
常见错误:使用前未清空栈
使用STL思路较为清晰
代码附上, 欢迎各位大神指点~~
- #include <cstdio>
- #include <stack>
- #include <iostream>
- #include <vector>
- using namespace std;
- stack<char> s;
- const int maxn = + ;
- char in[maxn], out[maxn]; //记录栈的原始次序, 出栈次序
- vector<string> str; //用以记录元素出入过程
- int solve(int n)
- {
- int A = , B = ;
- while(B < n){
- if(in[A] == out[B]){
- s.push(in[A++]);
- str.push_back("in");
- s.pop(); B++;
- str.push_back("out");
- }
- else if(!s.empty()&&s.top() == out[B]){
- s.pop();
- str.push_back("out");
- B++;
- }
- else if(A < n){
- s.push(in[A++]);
- str.push_back("in");
- }
- else return ;
- }
- if(s.empty()) return ;
- else return ;
- }
- void print()
- {
- for(vector<string>::iterator i = str.begin(); i != str.end(); i++){
- cout << *i << endl;
- }
- }
- int main()
- {
- int n;
- while(~scanf("%d", &n)){
- getchar();
- str.clear();
- memset(in, , sizeof(in));
- memset(out, , sizeof(out));
- while(!s.empty()) s.pop();
- scanf("%s%s", in, out);
- if(solve(n)){
- printf("Yes.\n");
- print();
- printf("FINISH\n");
- }
- else{
- printf("No.\n");
- printf("FINISH\n");
- }
- }
- return ;
- }
HDU1022 Train Problem I 栈的模拟的更多相关文章
- Train Problem I(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- train problem I (栈水题)
杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...
- Hdu 1022 Train Problem I 栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu1022 Train Problem I---模拟栈
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 题目大意: 车的进出站问题,先给出N个火车,再按序列一的方式进站,判断能否以序列二的方式出站,若 ...
- Train Problem(栈的应用)
Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- hdu1022 Train Problem I
http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...
- hdu 1022 Train Problem I【模拟出入栈】
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU Train Problem I 1022 栈模拟
题目大意: 给你一个n 代表有n列 火车, 第一个给你的一个字符串 代表即将进入到轨道上火车的编号顺序, 第二个字符串代表的是 火车出来之后到顺序, 分析一下就知道这,这个问题就是栈, 先进后出吗, ...
随机推荐
- 在Swift中的ASCII到字符转换的问题
我们在C++里处理字符通常是这样的 char a = 'A' // A = 65 printf("'%c' = %d", a + 1, a + 1) // 'B' = 66 这在号 ...
- C++11 之for 新解
前言 C++11这次的更新带来了令非常多C++程序猿期待已久的for range循环,每次看到javascript. lua里的for range.心想要是C++能有多好,心里别提多酸了.这 ...
- 商户怎样选择商业wifi进行移动营销
互联网移动营销的大潮席卷而来,带给很多其它的商户营销理念上的升级和更新.商业wifi营销成为很多其它的商户选择.以往,人们在咖啡店不过聊聊天喝喝咖啡,如今,人们能够更悠闲的歇息娱乐享受带有wifi的咖 ...
- python的print(转)
转载:http://www.pythonclub.org/python-basic/print 使用print输出各型的 字符串 整数 浮点数 出度及精度控制 strHello = 'Hello ...
- Matlab程序怎样打包
本人安装的版本号是MATLAB(R2010b) 打包过程例如以下: MATLAB命令窗体输入deploytool,打开一个Eeployment Project的窗体: 1.在Name输入你想要打包后的 ...
- poj 2688 状态压缩dp解tsp
题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...
- cocos2d-x中CCTextureCache图片资源的异步加载
如果没有预先加载图片,则可以通过addImageAsync()函数实现异步加载,该函数通过创建一个加载线程来加载图片,并且在主线程中通过调用回调函数来读取该图片资源纹理.其主要过程如下: 1.创建线程 ...
- MapReduce原理及其主要实现平台分析
原文:http://www.infotech.ac.cn/article/2012/1003-3513-28-2-60.html MapReduce原理及其主要实现平台分析 亢丽芸, 王效岳, 白如江 ...
- SIFT算法的应用--目标识别之Bag-of-words模型
原文:http://blog.csdn.net/v_JULY_v/article/details/6555899 SIFT算法的应用 -目标识别之用Bag-of-words模型表示一幅图像 作者:wa ...
- 基于Docker服务的java Web服务搭建
导读 最近想我们的应用需要更新维护,Android.IOS.还有服务器端都要更新,都在忙于写代码没有写文章了.我们的服务器是用java ssh架构的,到时也打算切换成Spring MVC+oauth2 ...