Title Description
Small, card games you played it? There is something called "Rama car" game, the rules are simple, very attractive children.
Its rules summarized as follows: Suppose the children participating in the game are A and B, when the game starts, the random playing card sequence they are as follows:
A side: [K, 8, X, K, A, 2, A, 9, . 5, a]
B Party: [2, 7, K, 5, J, 5, Q, 6, K, 4]
where X represents "10", we ignore the color of the card.
A side from the start, the two sides A, B turns the cards.
When it came out of a party card, he took his card from the head of a queue, on the table, and pressed on top of a card (if any).
In this embodiment, the game process: A a K, B a 2, A the 8, B out of 7, A the X, at this time the table is the sequence: K, 2,8,7, X
when the cards turn B when the same K and K his cards on the table card sequence, including K put the card, including the well between the two K are to win back into their own brand of the tail.
Note: For ease of operation, the order of the cards are placed in the order on the table opposite.
In this case, A, B both hands of cards is:
A side: [K, A, 2, A,. 9,. 5, A]
B Party: [5, J, 5, Q, 6, K, 4, K , X, 7, 8, 2 , K]
one winning hand of cards continues. Then B is a 5, A a K, B a J, A out of A, B out of 5, and the winning hand.
5, K, J, A, 5
at this time both hands of cards:
A side: [2, A, 9, 5, A]
B-side: [Q, 6, K, 4, K, X, 7, 8, 2, K, 5, A, J, K, 5]
Note: More time is not a winning hand one can table win cards are gone, but take the same card points and the middle part.
Nevertheless, the party winning hand is a continuation of the cards, sometimes just one card and won, is also permitted.
When the party out of the hands of the last card, but you can not win a license from the desktop, the game ends immediately.
For this example the initial hand, the last going to lose A and B cards for the last hand: 9K2A62KAX58K57KJ5
this task problem is known to both the initial sequence of the cards at the end of the calculation games, winning the one hand card sequence . When the game can not end, output -1.
Its rules summarized as follows: Suppose the children participating in the game are A and B, when the game starts, the random playing card sequence they are as follows:
A side: [K, 8, X, K, A, 2, A, 9, . 5, a]
B Party: [2, 7, K, 5, J, 5, Q, 6, K, 4]
where X represents "10", we ignore the color of the card.
A side from the start, the two sides A, B turns the cards.
When it came out of a party card, he took his card from the head of a queue, on the table, and pressed on top of a card (if any).
In this embodiment, the game process: A a K, B a 2, A the 8, B out of 7, A the X, at this time the table is the sequence: K, 2,8,7, X
when the cards turn B when the same K and K his cards on the table card sequence, including K put the card, including the well between the two K are to win back into their own brand of the tail.
Note: For ease of operation, the order of the cards are placed in the order on the table opposite.
In this case, A, B both hands of cards is:
A side: [K, A, 2, A,. 9,. 5, A]
B Party: [5, J, 5, Q, 6, K, 4, K , X, 7, 8, 2 , K]
one winning hand of cards continues. Then B is a 5, A a K, B a J, A out of A, B out of 5, and the winning hand.
5, K, J, A, 5
at this time both hands of cards:
A side: [2, A, 9, 5, A]
B-side: [Q, 6, K, 4, K, X, 7, 8, 2, K, 5, A, J, K, 5]
Note: More time is not a winning hand one can table win cards are gone, but take the same card points and the middle part.
Nevertheless, the party winning hand is a continuation of the cards, sometimes just one card and won, is also permitted.
When the party out of the hands of the last card, but you can not win a license from the desktop, the game ends immediately.
For this example the initial hand, the last going to lose A and B cards for the last hand: 9K2A62KAX58K57KJ5
this task problem is known to both the initial sequence of the cards at the end of the calculation games, winning the one hand card sequence . When the game can not end, output -1.
Entry
Input a plurality of sets of data, for each test:
input rows 2, 2 strings, respectively, both cards sequence A, B initial hands. The length of the input string is not more than 30
input rows 2, 2 strings, respectively, both cards sequence A, B initial hands. The length of the input string is not more than 30
Export
For each set of test data: 1 line output, a string, A represents the first play, the final hand card sequence one win.
Solution: use simulation like stacks and queues
#include<iostream> #include<string> #include<algorithm> #include<math.h> #include<string.h> #include<map> #include<stack> #include<queue> #define ll long long using namespace std; char a[100],b[100]; int main() { while(cin>>a>>b) { int len1=strlen(a); int len2=strlen(b); queue<char>aa; queue<char>bb; map<char,int>mp; for(int i=0;i<len1;i++) aa.push(a[i]); for(int i=0;i<len2;i++) bb.push(b[i]); stack<char>p; int ans=0,flag=0; while(!aa.empty()&&!bb.empty()) { if(flag==0) { char temp=aa.front(); aa.pop(); if(mp[temp]==1) { aa.push(temp); mp[temp]=0; while(1) { char x=p.top(); p.pop(); aa.push(x); mp[x]=0; if(x==TEMP) BREAK ; } In Flag ! = In Flag; // here again inverted, and then back again inverted back to its original value, it is realized "to continue after the winning cards" rule } the else { p.push (TEMP) ; MP [TEMP] = . 1 ; } } the else { char TEMP = bb.front (); bb.pop (); IF (MP [TEMP] == . 1 ) // If the stack inside the same letters p { BB. push (temp); MP [TEMP] = 0 ; the while(1) { char x=p.top(); p.pop(); bb.push(x); mp[x]=0; if(x==temp) break; } flag=!flag; } else { p.push(temp); mp[temp]=1; } } flag=!flag; ans++; } if(ans>10000) cout<<"-1"<<endl; else { if(aa.empty()) { while(!bb.empty()) { cout<<bb.front(); bb.pop(); } } else { while(!aa.empty()) { cout<<aa.front(); aa.pop(); } } cout<<endl; } } return 0; }