hdu_1022_Train Problem I_(模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022

题意:有n辆火车,给出入栈和出栈的顺序,编写程序判段出栈是否正确。

样例:3 123 132 是可以的

<span style="font-size:18px;">#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
    int n;
    char in[10],out[10];
    stack<char> s;
    int sign[20];//尽量大点,标记:1代表入栈,0代表出栈
    while(cin >> n >> in >> out)
    {
    	memset(sign,0,sizeof(sign));
    	//保证测试每组数据前都是空栈
        while(!s.empty())
        {
            s.pop();
        }
        int j = 0,k = 0;
        for(int i = 0;i < n;i++)
        {
            s.push(in[i]);
            sign[k++] = 1;
            while(!s.empty() && j < n)//空栈和出栈顺序访问完了
            {
                if(s.top() != out[j])
                {
                    break;
                }
                else
                {
                    s.pop();
                    sign[k++] = 0;
                    j++;
                }
            }
        }
        if(s.empty())
        {
            cout << "Yes." << endl;
            for(int i = 0;i < k;i++)//i < n会WA ,k是 “in” 和 “out ” 的总次数
            {
             	if(sign[i] == 1)
				 	cout << "in" << endl;
				else
					 cout << "out" << endl;
            }
            cout << "FINISH" << endl;
        }
        else
            cout << "No." << endl << "FINISH" << endl;
    }
    return 0;
}</span>
时间: 2024-10-27 09:05:20

hdu_1022_Train Problem I_(模拟)的相关文章

HDU 1022 Train Problem I 模拟栈题解

火车进站,模拟一个栈的操作,额外的栈操作,查看是否能按照规定顺序出栈. 数据量很少,故此题目很容易AC. 直接使用数组模拟就好. #include <stdio.h> const int MAX_N = 10; char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N]; bool rs[MAX_N<<2]; int n; int main() { while (scanf("%d", &n) != EOF) { s

HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 312    Accepted Submission(s): 219 Problem Description One day, you, a clever boy, feel bored in your math class, and then fall

G - Train Problem I(模拟题)

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 proble

HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You m

uva 101 The Blocks Problem (模拟)

uva 101  The Blocks Problem Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot arm per

csu 1549: Navigition Problem(几何,模拟)

1549: Navigition Problem Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 305  Solved: 90[Submit][Status][Web Board] Description Navigation is a field of study that focuses on the process of monitoring and controlling the movement of a craft or vehicle

HDU 4716 A Computer Graphics Problem(模拟啊 )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4716 Problem Description In this problem we talk about the study of Computer Graphics. Of course, this is very, very hard. We have designed a new mobile phone, your task is to write a interface to displa

HDU 1032 [The 3n + 1 problem] 暴力模拟

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 题目大意:给出i,j,要求输出i j之间"3N+1"的循环次数的最大值. 关键思想:暴力,模拟.可以优化,因为某些大数在进行操作时,会变为已经求过的小数,之后的循环次数已求过. 代码如下: //between i,j 模拟,暴力 #include <iostream> #include <algorithm> using namespace std; int

hdoj 2522 A simple problem 【模拟】

题意:算出1/n的结果,循环小数只输出第一个循环节 策略:模拟1除去n即可. 判断是否是循环节只需要找到%n之后的模是否出现就好了. 代码: #include <stdio.h> #include <string.h> #define M 100005 bool vis[M]; int main(){ int t, n; scanf("%d", &t); while(t --){ scanf("%d", &n); if(n =