Topic Title: Railway Stack Problem
The railway dispatching station is as follows:
The train number is: 1~9, and does not repeat.
Such as: The numbers are "1", "2", "3", "4", "5" of the 5 train station, then the pit stop sequence for "12345", all the station and then sequentially outbound, then the outbound sequence for "54321", if advanced 1, 2, then 2 outbound, then 1 outbound, then 3 station, outbound, 4 pit, Outbound, 5 Pit Stop, outbound, then the outbound sequence is 21345.
Detailed Description:
int judgetrainsequence (int maxnum, char *poutseq);
Input parameters:
int Maxnum: Maximum number of trains in the pit
char* Poutseq: Using strings to represent train outbound sequences
Output parameters (the memory area pointed to by the pointer is guaranteed to be valid):
No.
return value:
Int: Judging by the input pit sequence, if the input outbound sequence is possible, return 1, otherwise return 0;
Analysis + code:
This topic has been prompted is the problem of the stack, a bit of data structure based on the Wanderers know this into the stack out of the operation, the key point is that for an ordinal n into the stack, if there are less than n the number has not been out of the stack, then they must be in descending order after the stack. For every number is so, but for the subject is too simple, the fastest and most violent method is to simulate the stack operation, which is also acmer know the classic data structure algorithm, you can use the STL already defined stack operation, for the subject because the stack starting from 1 numbered, and is sequential, That is, the stack sequence is constant, so you can use the array to simulate it, first numbered 1 into the stack (into each element of the array), and then start from the current position of the arrays, and then compare and out the stack sequence of the corresponding elements are equal, until unequal, put the number 2 ... Similar, the code is very simple, easy to understand, time complexity is O (n^2), no time optimization, welcome replies to Exchange ~
#include <iostream>//#include <string>//#include <algorithm>//#include <cmath>//#include <vector>//#include <stack>//#include <iomanip>//using namespace std; #include <stdlib.h># Include <string.h> #include <stdio.h> /* Detailed description: int judgetrainsequence (int maxnum, char *poutseq); Input parameters: Int Maxnum: Train station Maximum number char* POUTSEQ: Use string to indicate train outbound sequence output parameters (the memory area pointed to by the pointer is guaranteed to be valid): None. return value: Int: Judging by the input pit sequence, if the input sequence is possible, return 1, otherwise return 0;*/int judgetrainsequence (Int maxnum, char *poutseq) {if (strlen (POUTSEQ) !=maxnum) return 0;char *ss= (char *) malloc (sizeof (char)), int i,j=0,k=0;for (i=1;i<=maxnum;i++) {ss[j]=i+ ' 0 '; while ( Ss[j]==poutseq[k] && k<maxnum) {k++;j--;} if (k==maxnum) return 1;j++;} return 0;} int main () {char *ss=null;printf ("%d\n", Judgetrainsequence (5, "53421")),//cout<<judgetrainsequence (5, "53421" ) <<endl;//12345;34215return 0;}
Huawei OJ Railway stack problem (analysis + source code)