8th provincial race java pull cart

Pull carriage


Problem description
When you were young, did you play card games? There is a game called "pull cart", the rules are very simple, but very attractive to children.
The rules are summarized as follows:
Suppose the children participating in the game are A and B. When the game starts, they get a random sequence of cards as follows:
Party A: [K, 8, X, K, A, 2, A, 9, 5, A]
Side B: [2, 7, K, 5, J, 5, Q, 6, K, 4]
where X means "10", and we ignore the suits of the cards. Starting from side A, both A and B take turns out of cards.
When it's a party's turn to play a card, he takes one from the head of his card queue, puts it on the table, and presses it on the top card (if any).
In this example, the game process:
A is out of K, B is out of 2, A is out of 8, B is out of 7, A is out of X, and the sequence on the table is:
K, 2, 8, 7 , X
when it is B's turn to play When his card K is the same as the K in the card sequence on the table, he will win back all the cards including the K and between the two K and put them at the end of his own card team. Note: For the convenience of operation, the order of placing cards is opposite to the order on the table.
At this time, the hands of A and B are:
A side: [K, A, 2, A, 9, 5, A]
B side: [5, J, 5, Q, 6, K, 4, K , X, 7, 8, 2, K] The
winning side continues to play. That is, B then played 5, A played K, B played J, A played A, B played 5, and won again.
5,K,J,A,5 Now the
cards in both hands:
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 often the winning side cannot put the table The cards are all won, but the same card points and the middle part are taken. But in any case, the winning side continues to play, and sometimes it wins as soon as the card is played, which is also allowed. When one party plays the last card in his hand but cannot win a card from the table, the game ends immediately. For the initial hand of this example, A will lose at the end, and the last card of B will be:
9K2A62KAX58K57KJ5
The task of this problem is to know the initial card sequence of both parties and calculate the card sequence of the winning side at the end of the game. . When the game cannot be ended, -1 is output.
The input is 2 lines, 2 strings, which respectively represent the sequence of cards in the initial hands of A and B.
The output is 1 line, 1 string, which means that the A's cards are played first, and the order of the cards in the hand of the winning side.
For example,
input:
96J5A898QA
6278A7Q973
, the program should output:
2J9A7QA6Q6889977. For
example,
input:
25663K6X7448
J88A5KJXX45A,
then the program should output:
6KAJ458KXAX885XJ645
We agree that the length of the input string does not exceed 30

Ideas:

I regard a and b as their current card order, and s means the card order on the table. over indicates whether the program ends or not over=0 ends, x is used to determine the card is played now, use if x0, if x1, to judge the cards of a and b. I use the pan(pop) function to determine whether the cards closed on the table and whether a, b still have cards in hand. Without over=0, the title says that when the game cannot be over, output -1. What I use is to end the program when the time exceeds 0.8 seconds, because the program enters an endless loop if it cannot be ended. Below is my idea map.

program:

import time
a=input()
b=input()
s=""
x=0
over=1
def pan(p):
    global s,x,a,b,over
    pop=""
    s+=p
    k=s.index(s[-1])
    if k!=len(s)-1:
        pop=s[k:]
        pop=pop[::-1]
        s=s[:k]
        if x==1:
            a+=pop
            x=0
        else:
            b+=pop
            x=1
    if a=="" or  b=="":
        if a=="":
            print(b)
        else:
            print(a)
        over=0
sj=time.time()
while 1:
    if x==0  and over==1:
        pop=a[0]
        a=a[1:]
        x=1
        pan(pop)
    if x==1 and over==1:
        pop=b[0]
        b=b[1:]
        x=0
        pan(pop)
    if over==0:
        break
    if time.time()-sj>0.8:
        print(-1)
        break

Reprinting is prohibited. Only for self-study. No responsibility for program errors.

Guess you like

Origin blog.csdn.net/weixin_46640345/article/details/112432510