Topic:
Description
The bored soldiers is playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, All values is different. They divide cards between them in some manner, it's possible that they has different number of cards. Then they play a "war"-like card game.
The rules are following. On each turn a fight happens. Each of the them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins This Fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the B Ottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.
Calculate how many fights would happen and who'll win the game, or state that game won ' t END.
Input
First line contains a single integer n (2≤ n ≤10), the number of cards.
Second line contains Integer K 1 (1≤ k 1≤ n -1), the number of the first soldier ' s cards. Then Follow K 1 integers that is the values on the first soldier ' s cards, from top to bottom O F his stack.
Third line contains integer k2 (k1 + k2 = n), the Numbe R of the second soldier ' s cards. Then follow k2 integers that is the values on the second soldier ' s cards, from top to Bott Om of his stack.
All card values is different.
Output
If Somebody wins in this game, print 2 integers where the first one stands for the number of Fights before end of game and the second one is 1 or 2 showing which player has won.
If the game won ' t end and would continue forever output -1.
The main idea : a K1 card, a person K2 card, two people to take their own top that card comparison, the cards of the two cards in small size under the order of the big under the pile, until one side did not stop. If the loop does not result. problem-solving ideas: the first kind of non-circular directly with the array and loop, but the judgment loop array is not, first put the card into a character array, and then set a map of the two-dimensional array, with the character to do subscript map integer, each time in the loop to determine whether the equality. Code:
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <string>5#include <map>6#include <Set>7#include <algorithm>8 using namespacestd; 9 Const intmaxn= -+ -;Tenmap<string,map<string,int> >p; One intMain () A { - intN; - intK1,k2; the intA[MAXN],B[MAXN],C[MAXN]; - CharS1[MAXN],S2[MAXN]; - inti,j,k; -scanf"%d",&N); +scanf"%d",&K1); - for(i =0; i<k1; i++) + { Ascanf"%d",&A[i]); atC[i] =A[i]; - } -scanf"%d",&K2); - for(i =0; i<k2; i++) - { -scanf"%d",&B[i]); inC[k1+i] =B[i]; - } toSort (c,c+k1+K2); + for(i =0; i<k1; i++) - { the for(j =0; j<k1+k2; J + +) * { $ if(a[i]==C[j])Panax NotoginsengS1[i] = j+'0'; - } the } +S1[K1] =' /'; A for(i =0; i<k2; i++) the { + for(j =0; j<k1+k2; J + +) - { $ if(b[i]==C[j]) $S2[i] = j+'0'; - } - } theS2[K2] =' /'; -P[S1][S2] =1; Wuyi into =0; the while(k1&&K2) - { Wu intP1 = s1[0],P2 = s2[0]; - if(p1>p2) About { $ for(i =0; i<k2; i++) -S2[i] = s2[i+1]; -k2--; - for(i =0; i<k1; i++) AS1[i] = s1[i+1]; +S1[k1-1] =P2; theS1[K1] =P1; -k1++; $S2[K2] = S1[k1] =' /'; the } the Else the { the for(i =0; i<k1; i++) -S1[i] = s1[i+1]; ink1--; the for(i =0; i<k2; i++) theS2[i] = s2[i+1]; Abouts2[k2-1] =P1; theS2[K2] =P2; thek2++; theS2[K2] = S1[k1] =' /'; + } - if(P[s1][s2]) the { Bayio =-1; the Break; the } -o++; -p [S1][S2] =1; the } theprintf"%d", O); the if(o!=-1) the { - if(K1) theprintf"1"); the Else theprintf"2"); 94 } theprintf"\ n"); the return 0; the}
Huas Summer training#2 B