stack HDU1022

 1 #include <iostream>
 2 #include <stack>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7 char target[10000];
 8 char source[10000];
 9 int arr[10000];
10
11 int main()
12 {
13     stack<char>s;
14     int n;
15     while(cin>>n)
16     {
17         while(!s.empty())
18             s.pop();
19         memset(arr,0,sizeof(arr));
20         cin>>source;
21         cin>>target;
22         int t1=0;
23         int t2=0;
24         int flag=1;
25         int tt=1;
26         while(t1<n)
27         {
28            if(source[t2]==target[t1])
29            {
30                arr[tt]=1;
31                tt++;
32                arr[tt]=2;
33                tt++;
34                t1++;
35                t2++;
36            }
37            else if(!s.empty()&&s.top()==target[t1])
38            {
39                arr[tt]=2;
40                tt++;
41                s.pop();
42                t1++;
43            }
44            else if(t2<n)
45            {
46                s.push(source[t2]);
47                arr[tt]=1;
48                tt++;
49                t2++;
50            }
51            else
52            {
53                flag=0;
54                break;
55            }
56         }
57         if(tt==2*n+1)
58         {
59             cout<<"Yes.\n";
60             for(int i=1;i<tt;i++)
61             {
62                 if(arr[i]==1)
63                     cout<<"in\n";
64                 if(arr[i]==2)
65                     cout<<"out\n";
66             }
67             cout<<"FINISH\n";
68         }
69         else
70         {
71             cout<<"No.\n";
72             cout<<"FINISH\n";
73         }
74     }
75     return 0;
76 }

时间: 2024-10-08 21:10:06

stack HDU1022的相关文章

HDU1022 Train Problem I (栈)

栈+队列 1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<queue> 5 using namespace std; 6 int main() 7 { 8 int n; 9 char a[11],b[11]; 10 stack<char>s; 11 queue<int>q; 12 while(scanf("%d",&

hdu1022 train problem 栈的应用

#include #include #include using namespace std; int main() { int n; while(cin >> n) { stack one; string od1,od2; bool state[10001]; cin >> od1 >> od2; int from = 0 , to = 0; int i = 0; while(from < n) { while(od1[from] != od2[to]) one

51nod1289 stack

1289 大鱼吃小鱼 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右).问足够长的时间之后,能剩下多少条鱼? Input 第1行:1个数N,表示鱼的数量(1 <= N <= 100000). 第2 - N + 1行:每行两个数A[i], 

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu

About LAMP LAMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Apache, MySQL, and PHP. Since the virtual private server is already running Ubuntu, the linux part is taken care of. Here i

Stack的pop和push操作

#include <stack> #include <cstdio> using namespace std; int main(){ stack<int> s; s.push(1); s.push(2); s.push(3); printf("%d\n", s.top()); s.pop(); printf("%d\n", s.top()); s.pop(); printf("%d\n", s.top());

从头认识java-9.8 栈(Stack)

这一章节我们来讨论一下栈(Stack). 1.特性 先进后出,当一个元素压进栈里面,他就会处于栈的底部,然后,另一个再压进来,盖在原来的元素上面,原来的元素想出去,只有等上面的元素先顶出栈才有机会. 2.方法演示 package com.ray.ch09; import java.util.Arrays; import java.util.Stack; public class Test { public static void main(String[] args) { Stack<Integ

C#中泛型容器Stack&lt;T&gt;的用法,以及借此实现&rdquo;撤销/重做&rdquo;功能

.Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T>,双向链表的泛型实现,不支持索引;ISet<T>不允许被复制,他有2个实现,一个是HashSet<T>,不维持集合元素的排序,另一个是SortedSet<T>,支持集合元素的排序;IDictionary<TKey, TValue>是一个字典集合的泛型接口

Improving performance – A full stack problem

Improving performance – A full stack problem March 6, 2015 by ronald 4 Comments Improving the performance of a web system involves knowledge of how the entire technology stack operates and interacts. There are many simple and common tips that can pro

LeetCode Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e