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
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
const int INF = 1 << 28;

int N, M;
void a_sit(string &s) {
  for(int i=0; i<N; i++) {
    if(s[i] == '#') {
      s[i] = 'A';
      return;
    }
  }
}

void b_sit(string &s) {
  for(int i=N-1; i>=0; i--) {
    bool ok = (s[i] == '#');
    ok &= (i+1 >= N or s[i+1] != 'A');
    ok &= (i-1 <  0 or s[i-1] != 'A');

    if(ok) {
      s[i] = 'B';
      return;
    }
  }

  for(int i=0; i<N; i++) {
    if(s[i] == '#') {
      s[i] = 'B';
      return;
    }
  }
}

void c_sit(string &s) {
  for(int i=0; i<N; i++) {
    if(s[i] != '#') {
      if(i+1 < N and s[i+1] == '#') {
        s[i+1] = 'C';
        return;
      }
      if(i-1 >= 0 and s[i-1] == '#') {
        s[i-1] = 'C';
        return;
      }
    }
  }

  s[N/2] = 'C';
  return;
}

void d_sit(string &s) {
  int ma = -1, arg = -1;
  for(int i=0; i<N; i++) {
    if(s[i] != '#') continue;
    int min_dist = INF;
    for(int j=0; j<N; j++) {
      if(i == j or s[j] == '#') continue;
      int dist = (i - j > 0 ? i - j : j - i);
      if(min_dist > dist) {
        min_dist = dist;
      }
    }

    if(min_dist != INF and ma < min_dist) {
      ma = min_dist;
      arg = i;
    }
  }

  if(arg < 0) {
    s[0] = 'D';
  }
  else {
    s[arg] = 'D';
  }
}

void sit_down(char x, string &s) {
  if(x == 'A') a_sit(s);
  if(x == 'B') b_sit(s);
  if(x == 'C') c_sit(s);
  if(x == 'D') d_sit(s);
}

int solve() {
  cin >> N >> M;
  if(N == 0 and M == 0) return 1;

  vector<char> A(M);
  string ans(N, '#');
  for(int i=0; i<M; i++) cin >> A[i];
  for(int i=0; i<M; i++) {
    sit_down(A[i], ans);
  }
  cout << ans << endl;
  return 0;
}

int main() {
  while(!solve());
  return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
#define BLANK '#'
int main()
{
  int n,m;
  int i,j,blank,cnt,max;
  bool f;
  char c;
  vector<char> seat;
  while(cin>>n>>m,n|m){
    seat.resize(n);
    for(i=0;i<n;i++)seat[i]=BLANK;
    while(m-->0){
      cin>>c;
      switch(c){
	// A
	case 'A':
	  for(i=0;i<n;i++)
	    if(seat[i]==BLANK){seat[i]=c;break;}
	  break;
	// B
	case 'B':
	  for(i=n;i-->0;){
	    if(seat[i]==BLANK){
	      f=true;
	      if(i+1<n&&seat[i+1]=='A')f=false;
	      if(i>0&&seat[i-1]=='A')f=false;
	      blank=i;
	      if(f){seat[blank]=c;break;}
	    }
	  }
	  if(seat[blank]==BLANK)seat[blank]=c;
	  break;
	// C
	case 'C':
	  f=true;
	  for(i=0;i<n;i++)if(seat[i]!=BLANK)f=false;
	  if(f){seat[n/2]=c;break;}
	  for(i=0;i<n;i++){
	    if(seat[i]!=BLANK){
	      if(i+1<n&&seat[i+1]==BLANK){seat[i+1]=c;break;}
	      if(i>0&&seat[i-1]==BLANK){seat[i-1]=c;break;}
	    }
	  }
	  break;
	// D
	case 'D':
	  max=-1;
	  for(i=0;i<n;i++){
	    if(seat[i]==BLANK){
	      for(cnt=0,j=i;j-->0;cnt++)if(seat[j]!=BLANK)break;
	      if(max<cnt){max=cnt;blank=i;}
	      for(cnt=0,j=i;j++<n;cnt++)if(seat[j]!=BLANK)break;
	      if(max<cnt){max=cnt;blank=i;}
	    }
	  }
	  seat[blank]=c;
	  break;
      }
    }
    for(i=0;i<n;i++)cout<<seat[i];
    cout<<endl;
  }
}