Go: package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
		"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		chair := make([]string, n+1)
		for i := range chair {
			chair[i] = "#"
		}
		chair[n] = "Sentinel"
		for i := 0; i < m; i++ {
			scanner.Scan()
			switch who := scanner.Text(); who {
			case "A": // 左から眺めて行って空いている席があったら即座に座る
				for j := 0; j < n; j++ {
					if chair[j] == "#" {
						chair[j] = "A"
						break
					}
				}
				// log.Println(chair)
				break // 次の人を読み込む
			case "B":
				flag := true
				// 右端からAの隣でない席を探していく
				for j := n-1; j > -1; j-- {
					if chair[j] == "#" && (j == 0 || chair[j-1] != "A") && chair[j+1] != "A" {
						flag = false
						chair[j] = "B"
						break
					}
				}
				// Aの隣しか空いていないので一番左端の空いている席に座る
				if flag {
					for j := 0; j < n; j++ {
						if chair[j] == "#" {
							chair[j] = "B"
							break
						}
					}
				}
				// log.Println(chair)
				break // 次の人を読み込む
			case "C":
				if i == 0 {
					chair[n/2] = "C" // 先客が誰もいないときは真ん中の椅子に座る
				} else {
					for j := 0; j < n; j++ { // 左から舐めて
						if chair[j] != "#" { // その席が空いていなくて
							if chair[j+1] == "#" { // 右隣が空いているなら座る
								chair[j+1] = "C"
								break
							} else if j != 0 && chair[j-1] == "#" { // 右隣が空いていないので左隣に座る
								chair[j-1] = "C"
								break
							}
						}
					}
				}
				// log.Println(chair)
				break // 次の人を読み込む
			case "D":
				distanceTable := make([]int ,n)
				for j := range distanceTable {
					distanceTable[j] = INF
				}
				for j:= 0; j < n; j++ {
					if chair[j] != "#" {
						for k := 0; k < n; k++ {
							distanceTable[k] = min(distanceTable[k], abs(j-k))
						}
					}
				}
				// log.Println("distanceTAble:", distanceTable)
				at := 0
				distance := 0
				for j := 0; j < n; j++ {
					if distance < distanceTable[j] {
						at = j
						distance = distanceTable[j]
					}
				}
				chair[at] = "D"
				// log.Println(chair)
				break // 次の人を読み込む
			}
		}
		for i := 0; i < n; i++ {
			if i == n-1 {
				fmt.Println(chair[i])
			} else {
				fmt.Printf("%s ", chair[i])
			}
		}
	}
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

Python: INF = 10 ** 20

while True:
  n, m = map(int, input().split())
  if not n:
    break

  chairs = list("#" * n)

  def sit(s):
    if s == "A":
      chairs[chairs.index("#")] = "A"
    elif s == "B":
      for i in range(n - 1, -1, -1):
        if chairs[i] == "#" and (i == 0 or chairs[i - 1] != "A") and (i == n - 1 or chairs[i + 1] != "A"):
          chairs[i] = "B"
          break
      else:
        chairs[chairs.index("#")] = "B"

    elif s == "C":
      for i in range(1, n):
        if chairs[i] == "#" and chairs[i - 1] != "#":
          chairs[i] = "C"
          break

    else:
      score = [INF for _ in range(n)]
      dist = INF
      for i in range(n):
        if chairs[i] == "#":
          dist += 1
        else:
          dist = 0
        score[i] = dist

      dist = INF
      for i in range(n - 1, -1, -1):
        if chairs[i] == "#":
          dist += 1
        else:
          dist = 0
        score[i] = min(score[i], dist)
      high_score = max(score)
      chairs[score.index(high_score)] = "D"
  
  first = input()
  if first in ["A", "D"]:
    chairs[0] = first
  elif first == "B":
    chairs[-1] = "B"
  else:
    chairs[n // 2] = "C"
  for _ in range(m - 1):
    sit(input())

  print("".join(chairs))

Python: INF = 10 ** 20

def main():
  while True:
    n, m = map(int, input().split())
    if not n:
      break
  
    chairs = list("#" * n)
  
    def sit(s):
      if s == "A":
        chairs[chairs.index("#")] = "A"
      
      elif s == "B":
        for i in range(n - 1, -1, -1):
          if chairs[i] == "#" and (i == 0 or chairs[i - 1] != "A") and (i == n - 1 or chairs[i + 1] != "A"):
            chairs[i] = "B"
            break
        else:
          chairs[chairs.index("#")] = "B"
  
      elif s == "C":
        for i in range(n):
          if chairs[i] != "#":
            if i + 1 <= n - 1 and chairs[i + 1] == "#":
              chairs[i + 1] = "C"
              break
   
            elif i - 1  >= 0 and chairs[i - 1] == "#":
              chairs[i - 1] = "C"
              break
  
      else:
        score = [INF for _ in range(n)]
        dist = INF
        for i in range(n):
          if chairs[i] == "#":
            dist += 1
          else:
            dist = 0
          score[i] = dist
  
        dist = INF
        for i in range(n - 1, -1, -1):
          if chairs[i] == "#":
            dist += 1
          else:
            dist = 0
          score[i] = min(score[i], dist)
        high_score = max(score)
  
        chairs[score.index(high_score)] = "D"
    
    first = input()
    if first in ["A", "D"]:
      chairs[0] = first
    elif first == "B":
      chairs[-1] = "B"
    else:
      chairs[n // 2] = "C"
    
    for _ in range(m - 1):
      sit(input())
    print("".join(chairs))

main()

Python: def left_empty(bench):
    return bench.index('#') 

def a_sit(bench):
    bench[left_empty(bench)] = 'A'
def right_empty(bench):
    for i in reversed(range(len(bench))):
        if bench[i] == '#':
            yield i

def a_next(bench, i):
    if i + 1 < len(bench) and bench[i + 1] == 'A':
        return True
    if 0 <= i - 1 and bench[i - 1] == 'A':
        return True
    return False
            
def b_sit(bench):
    for i in right_empty(bench):
        if not a_next(bench,i):
            bench[i] = 'B'
            return
    bench[left_empty(bench)] = 'B'
def no_people(bench):
    for bi in bench:
        if bi != '#':
            return False
    return True

def not_empty(bench):
    for i in range(len(bench)):
        if bench[i] != '#':
            yield i

def c_sit(bench):
    if no_people(bench):
        bench[len(bench) // 2] = 'C'
        return
    for i in not_empty(bench):
        if i + 1 < len(bench) and bench[i + 1] == '#':
            bench[i + 1] = 'C'
            return
        if 0 <= i - 1 and bench[i - 1] == '#':
            bench[i - 1] = 'C'
            return
def d_sit(bench):
    if no_people(bench):
        bench[0] = 'D'
        return
    tmp = bench[:]
    
    dist = float('inf')
    for i in range(len(tmp)):
        if tmp[i] == '#':
            dist += 1
        else:
            dist = 0
        tmp[i] = dist
    dist = float('inf')
    for i in reversed(range(len(tmp))):
        if tmp[i] != 0:
            dist += 1
        else:
            dist = 0
        tmp[i] = min(tmp[i], dist)
    bench[tmp.index(max(tmp))] = 'D'

import sys
f = sys.stdin


sit = {'A':a_sit,'B':b_sit,'C':c_sit,'D':d_sit}
while True:
    m, n = map(int, f.readline().split())
    if n == 0:
        break
    bench = ['#'] * m
    for i in range(n):
        sit[f.readline().strip()](bench)
    print(''.join(bench))