[HDU] [1022] Train Problem I

Problem Description

Loading...

Problem Analysis

It is easy to think of simulating this with a stack
Here two arrays are used to store the data
and then they are compared
(and from that we can either pop right away, or keep the element and see whether it can be popped after the next one is processed)
and a flag array is used to record the required operations

Finally, compare the number of popped elements with the number of data items
if they are equal, the operation is valid

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
#include<stack>
using namespace std;
int main() {
	int n;
	char s1[15],s2[15];
	while(scanf("%d %s %s",&n,s1,s2)!=EOF) {
		stack<char>a;
		bool flag[30];
		int k=0,j=0;
		for(int i=0; i<n; i++) {
			a.push(s1[i]);
			flag[k++]=true;
			while(!a.empty() && a.top()==s2[j]) {
				flag[k++]=false;
				j++;
				a.pop();
			}
		}
		if(j==n) {
			printf("Yes.\n");
			for(int i=0; i<k; i++) {
				if(flag[i])
					printf("in\n");
				else
					printf("out\n");
			}
		} else
			printf("No.\n");
		printf("FINISH\n");
	}
	return 0;
}

Problem address:【杭电】[1022]Train Problem I

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...