Acm-train problem I

Source: Internet
Author: User
Tags time limit
D-train problem I
Time limit:1000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u
Submit

Status
Description
As the new term comes, the Ignatius Train station is very busy nowadays. A lot of student want-get back-to-school by train (because the trains in the Ignatius train station are the fastest all O Ver the world ^v^). But here comes a problem, there are only one railway where all the trains stop. The trains come in from one side and get off from the other side. For this problem, if train a gets into the railway first, and then train B gets into the railway before train A leaves, tr Ain A can ' t leave until train B leaves. The pictures below figure out the problem. Now the problem for your is, there was at the trains of the station and all the trains have an ID (numbered from 1 to N), the Trains get into the railway in a order O1, your task is to determine whether the trains can get out of an order O2.



Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and strings, the order of the trains come in:o1, and the Order of the trains Leave:o2. The input is terminated by the end of file. More details in the Sample Input.

Output
The output contains a string "No." If you can ' t exchange O2 to O1, or you should output a line contains "Yes.", and then O Utput your on exchanging the order (you should output "in" for a train getting to the railway, and "out" for a train Getting out of the railway). Print a line contains ' FINISH ' after each test case. More details in the Sample Output.

Sample Input
3 123 321
3 123 312

Sample Output
Yes.
Inch
Inch
Inch
Out
Out
Out
FINISH
No.
FINISH
Hint
Hint for the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 are at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we had to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can ' t let train 1 leave before train 2, because train 2 are at the top of the railway at the moment.

So we output "No.".


<p> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <algorithm> #include <vector> #include <string> #include <map> #include <stack> using
namespace Std;
stack<char>s;
void Init () {    while (!s.empty ())     s.pop ();} int main () {    int n,m;     while (scanf ("%d", &n)!=eof)     {       
Char a[100],b[100];
        int c[110];         scanf ("%s%s", &a,&b);</p><p>   
     init ();
        int k=0,i=0,j=0,r=0;         while (1) {</p><p>             while (I<n&&a[i]!=b[j])             {              
  S.push (A[i]);
                i++;                 c[r++]=1;</p> <p>           }              if (i<n)           
  {                s.push (a[i]);
                i++;
                c[r++]=1;            }              while (j<n&& (!s.empty ()) &&s.top () ==b[j])              {               
S.pop ();
                c[r++]=0;
                J + +;            }</p><p>             if (i==n| |
J==n) break;        }</p><p>        if (j== N) {            printf ("yes.\n");              for (int i=0;i<r;i++)                  if (C[i]) printf("in\n");
            Else printf ("out\n");
       }         Else printf ("no.\n");         printf ("finish\n");</p><p>        }</p><p>    }</p><p>  </p>

Advanced, encountered equal time to go in, and then prepare the stack, out of the stack to empty or stack top not equal to the next number.

Again into the stack, repeated loops, 1 represents the stack, 0 represents the stack.

If the stack cannot be stacked after all, then the sample error, I=n, break.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.