两个指针即可

公司食堂

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)





全部评论

相关推荐

02-12 00:59
已编辑
哈尔滨工业大学 产品经理
华为 软件开发岗 20.6*16薪 本科
点赞 评论 收藏
分享
02-11 12:20
门头沟学院 Java
面试中的青提很胆小:我不信有比我们学校更逆天的,计算机专业就业第一位是我们学校二餐厅的打印店
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务