两个指针即可
公司食堂
http://www.nowcoder.com/questionTerminal/601815bea5544f389bcd20fb5ebca6a8
T = int(input().strip()) import heapq def find_next(tables, i, num): tlen = len(tables) while i < tlen and tables[i] != num: i += 1 return i for i in range(T): N = int(input().strip()) tables = list(map(int, list(input().strip()))) M = int(input().strip()) sex = list(input().strip()) t0, t1 = [], [] p0, p1 = -1, -1 for i in range(N): if p0!=-1 and p1 != -1: break if p0 ==-1 and tables[i] == 0: p0 = i elif p1 == -1 and tables[i] == 1: p1 = i for i in range(M): if sex[i] == 'M': if p1 != N: print(p1+1) tables[p1] = 2 p1 = find_next(tables, p1+1, 1) else: print(p0+1) tables[p0] = 1 p1 = min(p0, p1) p0 = find_next(tables, p0+1, 0) else: if p0 != N: print(p0+1) tables[p0] = 1 p1 = min(p0, p1) p0 = find_next(tables, p0+1, 0) else: print(p1+1) tables[p1] = 2 p1 = find_next(tables, p1+1, 1)