The stack order of the stack and reasonable (boxcars scheduling)

Scheduling railroad cars

A stack may be more obvious pop title sequence variant, the stack 2, the pull 3 is a sequence, it becomes a water applications

        1  ======   <--移动方向
         /
 3 =====  
         \
        2  ======   -->移动方向 

Perhaps you've seen "column compartment scheduling problems" on certain data structures textbook (of course, I have not seen it does not matter). Today, we have to practice a following carriage scheduling. FIG ASCII characters above control, the problem is described as follows:

Train has three parallel tracks (2, 3) and two connecting tracks 1-3 and 2-3. A conventional carriage parked on the track No. 1, please use two connection tracks and track No. 3, the carriage 2 is transferred to the rail in the desired order. The rule is:

  • Section 1 each transfer carriage;
  • No. 1 in the carriage 1-3 is connected via the rail or track to enter the track number 3 (this operation referred to as "1-> 3"), or through direct access to the two tracks 2 connected to the track (this operation is referred to as "1 > 2 ");
  • Once the carriage 2 into the track, the track will not be further removed;
  • No. 3 in the track carriage, track only after 2-3 connected into the track number 2 (the operation is referred to as "3-> 2");
  • Obviously, any car can not pass through, or moved across bypass the other compartments.

For a given parking sequence number 1, if the sequence number can be realized through the scheduling requirements of the track 2, the operation sequence is given; if not, to ask the user Are (you) you (a) kidding (Kaiding) Me (what) ?

Input formats:

Two lines of non-empty string of uppercase letters, the first row indicates the order number of stops on the track of the vehicle compartment 1 from left to right, the second row represents a carriage stop in claim 2 to the track into the path order (input sample 1. the second row CBArepresents the cabin from left to right in the parking orbit is No. 2 ABC, because Cthe first to enter, so the far right). Two lines of the same length of the string and not more than 26 (26 since only uppercase letters), each letter represents a carriage. Title ensure letters will not be repeated in the same row and the same set of letters in two rows.

Output formats:

If successful scheduling, given the shortest possible sequence of operations, each operation per line. The so-called "shortest", i.e., if the 1-> 2 scheduling can be done, do not by 1-> 3 and 3-> 2 is achieved. If you can not schedule, output "Are you kidding me?"

Sample Input 1:

ABC
CBA

Output Sample 1:

1->3
1->3
1->2
3->2
3->2

Sample Input 2:

ABC
CAB

Output Sample 2:

Are you kidding me?
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char str[100];  
int str1[100],str2[100],stack[30],out[100],inout,index1,index2,top;  
int main()
{
//      freopen("test.in","r",stdin);
//      freopen("test.out","w",stdout);
    int i;
    bool flag=1;
    cin>>str;  
    int len=strlen(str); 
    for(i=0;i<len;i++)
        str1[i]=str[i]-'A';
    cin>>str;   
    for(i=0;i<len;i++)
        str2[i]=str[i]-'A'; 
    index1=0;
    index2=0;
    while(1)
    {
        if(index1<len and str1[index1]==str2[index2])
        {
            index1++;
            index2++; 
            out[inout]=12;
            inout++; 
        }
        else if(top!=0 and stack[top-1]==str2[index2])
        {
            top--;
            index2++; 
            out[inout]=32;
            inout++; 
        }
        else
        {
            if(index2==len)
                break;
            stack[top]=str1[index1];
            top++; 
            index1++;
            out[inout]=13;
            inout++;  
            if(index1>=len) 
            { 
                flag=0;
                break;
            }
        } 
    }
    if (flag==0 or top!=0)
        printf("Are you kidding me?\n");
    else
        for(i=0;i<inout;i++)
        { 
            if (out[i]==12)
              printf("1->2\n");
            else 
            if (out[i]==13)
              printf("1->3\n");
            else if(out[i]==32)
                printf("3->2\n");
        } 
    return 0;
}

Guess you like

Origin www.cnblogs.com/IamIron-Man/p/11957006.html