hdu 1022 模拟栈

其实就是模拟一下栈啦。

 1 #include <iostream>
 2 using namespace std;
 3
 4 const int N = 10;
 5 char o1[N];
 6 char o2[N];
 7 char s[N];
 8 int ans[N * 2];
 9
10 int main ()
11 {
12     int n;
13     while ( cin >> n )
14     {
15         cin >> o1 >> o2;
16         int top = 0, p = 0, cnt = 0;
17         for ( int i = 0; i < n; i++ )
18         {
19             if ( top == 0 )
20             {
21                 s[top++] = o1[p++];
22                 ans[cnt++] = 1;
23             }
24             while ( s[top - 1] != o2[i] )
25             {
26                 if ( p == n ) break;
27                 s[top++] = o1[p++];
28                 ans[cnt++] = 1;
29             }
30             if ( s[top - 1] != o2[i] )
31             {
32                 break;
33             }
34             else
35             {
36                 top--;
37                 ans[cnt++] = 0;
38             }
39         }
40         if ( cnt == 2 * n )
41         {
42             cout << "Yes." << endl;
43             for ( int i = 0; i < cnt; i++ )
44             {
45                 cout << ( ans[i] ? "in" : "out" ) << endl;
46             }
47         }
48         else
49         {
50             cout << "No." << endl;
51         }
52         cout << "FINISH" << endl;
53     }
54     return 0;
55 }
时间: 2024-12-25 04:16:29

hdu 1022 模拟栈的相关文章

HDU 1022 (栈的基本应用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22954    Accepted Submission(s): 8652 Problem Description As the new term com

Train Problem I hdu 1022(栈)

http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 题意:给出火车的进站与出站顺序,判断是否可以按照给出的出站顺序出站. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algorithm> #include <ma

hdu 1022 - 数据结构 栈

题目链接 按序列a进栈,问能不能按序列b出栈. 遍历b,如果当前元素进过栈了,那么必须和栈顶元素相同.如果没进过栈则按a序列压栈,直到遇到当前元素. #include <iostream> #include <stack> #include <string.h> using namespace std; char ino[22],outo[22]; bool visited[22]; char result[222]; int n; int main(){ int 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 1022 Train Problem I (数据结构 —— 栈)

Problem 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

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

HDU 3328 Flipper 栈 模拟

首先想说,英语太烂这题读了很长时间才读懂......题意是说输入有几张牌,然后输入这些牌的初始状态(是面朝上还是面朝下),然后输入操作方式,R表示翻一下右边的牌堆,L表示翻一下左边的牌堆,直到最后摞成了一个牌堆.后面跟着一排问题,输入i,问从上往下数的第i张牌编号,以及这张牌的状态. 这题只要读懂了题意思路也挺清晰的了,用栈进行模拟一步一步走就行了,我的思路是先对牌的状态进行计算,如果是R,就把这一步右边所有牌都翻过来,如果是L,就把这一步左边所有牌都翻过来,这样先把所有牌编号和状态都对应好了,

java 16 - 5 LinkedList模拟栈数据结构的集合

请用LinkedList模拟栈数据结构的集合,并测试 题目的意思是: 你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟. 1 package cn_LinkedList; 2 3 import java.util.LinkedList; 4 5 6 7 8 public class MyStack { 9 10 //定义一个LinkedList类的成员变量 11 private LinkedList list = null; 12 13 /** 14 * 构造方法 15

用数组模拟栈的结构

package datastruct; import java.util.Arrays; /** * 用数组模拟栈的结构:后进先出(LIFO) 线性表结构 * @author stone * 2014-07-29 06:34:49 */ public class SimulateStack<E> { public static void main(String[] args) { SimulateStack<String> ss = new SimulateStack<Str