n = int(input()) drops = input() picks = input() cases = [] dropc = drops.count('C') dropm = drops.count('M') for i in range(6): cases.append([]) picksc = 0 picksm = 0 for i in range(n): pick = picks[i] if picks[i] == 'C': picksc += 1 if picksc > dropc: pick = '-' elif picks[i] == 'M': picksm += 1 if picksm > dropm: pick = '-' if drops[i] == '-': if pick == 'M': cases[4].append(i+1) elif pick == 'C': cases[5].append(i+1) elif drops[i] == 'C': if pick == 'M': cases[3].append(i+1) elif pick == '-': cases[0].append(i+1) elif drops[i] == 'M': if pick == 'C': cases[2].append(i+1) elif pick == '-': cases[1].append(i+1) cnts = [len(c) for c in cases] paths = [] #first cut off one of the MC or CM cases by a big cycle if cnts[2] < cnts[3]: # must have a M- and -C to be solvable path = [] path.append(5) cnts[5] -= 1 c = cnts[2] for i in range(c): path.append(3) cnts[3] -= 1 path.append(2) cnts[2] -= 1 path.append(3) cnts[3] -= 1 path.append(1) cnts[1] -= 1 paths.append(path) # solve remaining CM if theyre there rm = cnts[3] for i in range(rm): paths.append([5, 3, 1]) cnts[5] -= 1 cnts[3] -= 1 cnts[1] -= 1 elif cnts[2] > 0 and cnts[2] == cnts[3]: #check if we use -C C- or -M M- path = [] if cnts[5] > 0: path.append(5) cnts[5] -= 1 rm = cnts[2] for i in range(rm): path.append(3) cnts[3] -= 1 path.append(2) cnts[2] -= 1 path.append(0) cnts[0] -= 1 else: path.append(4) cnts[4] -= 1 rm = cnts[2] for i in range(rm): path.append(2) cnts[2] -= 1 path.append(3) cnts[3] -= 1 path.append(1) cnts[1] -= 1 paths.append(path) elif cnts[2] > cnts[3]: #must have a C- and -M to be solvable path = [] path.append(4) cnts[4] -= 1 c = cnts[3] for i in range(c): path.append(2) cnts[2] -= 1 path.append(3) cnts[3] -= 1 path.append(2) cnts[2] -= 1 path.append(0) cnts[0] -= 1 paths.append(path) #solve remaining MC if theyre there rm = cnts[2] for i in range(rm): paths.append([4,2,0]) cnts[4] -= 1 cnts[2] -= 1 cnts[0] -= 1 #solve remaining -C C- and -M M- combos rm = cnts[0] for i in range(rm): paths.append([5,0]) cnts[5] -= 1 cnts[0] -= 1 rm = cnts[1] for i in range(rm): paths.append([4,1]) cnts[4] -= 1 cnts[1] -= 1 #output the actual route ixs = [0] * 6 if len(paths) == 0: print(0) else: res = "" lenres = 0 for path in paths: for ix in range(len(path)): cs = path[ix] loc = cases[cs][ixs[cs]] ixs[cs] += 1 res += "DRIVE " + str(loc) + "\n" lenres += 1 if ix == 0: res += "PICKUP\n" lenres += 1 elif ix == len(path) - 1: res += "DROPOFF\n" lenres += 1 else: res += "DROPOFF\nPICKUP\n" lenres += 2 print(lenres) print(res, end = '')