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 // 次の人を読み込む
			}
		}
		fmt.Println(strings.Join(chair, ""))
	}
}

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>

int main() {
	int n, m;
	const char output[] = { '#', 'A', 'B', 'C', 'D' };
	std::vector<int> seat;
	
	while (std::cin >> n >> m) {
		if (n == 0 && m == 0)
			break;

		seat.resize(n);

		for (int i = 0; i < m; ++i) {
			char who;
			std::cin >> who;

			if (who == 'A') {
				for (int j = 0; j < n; ++j) {
					if (seat[j] == 0) {
						seat[j] = 1;
						break;
					}
				}
			}
			else if (who == 'B') {
				int firstEmpty = 334;
				bool isRightA = false;

				for (int j = n - 1; j >= 0; --j) {
					if (seat[j] == 0) {
						firstEmpty = std::min(firstEmpty, j);
						if (!isRightA && (j == 0 || seat[j - 1] != 1)) {
							seat[j] = 2;
							break;
						}
					}
					isRightA = seat[j] == 1;
				}
			}
			else if (who == 'C') {
				for (int j = 0; j < n; ++j) {
					if (seat[j] != 0) {
						if (j != n - 1 && seat[j + 1] == 0) {
							seat[j + 1] = 3;
							break;
						}
						if (j != 0 && seat[j - 1] == 0) {
							seat[j - 1] = 3;
							break;
						}
					}
					if (j == n - 1)
						seat[n / 2] = 3;
				}
			}
			else if (who == 'D') {
				int maxDist = 0;
				int maxIdx = 0;

				int dist = 0;

				int fromTop = 0;
				int toLast = 0;

				for (int j = 0; j < n; ++j) {
					++dist;
					if (seat[0] != 0 && j == n - 1)
						toLast = dist;

					if (seat[j] != 0) {
						if (fromTop == 0)
							fromTop = j + 1;

						if (maxDist < dist / 2) {
							maxDist = dist / 2;
							maxIdx = j - dist;
						}
						dist = 0;
					}
				}

				if (seat[n - 1] == 0 && toLast > maxDist && fromTop < toLast)
					seat[n - 1] = 4;
				else if (seat[0] == 0 && fromTop > maxDist && fromTop > toLast)
					seat[0] = 4;
				else
					seat[maxIdx + maxDist] = 4;
			}
		}

		for (int i = 0; i < n; ++i)
			std::cout << output[seat[i]];
		std::cout << std::endl;
		seat.clear();
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int N, M;
int a[100];

void sit_from_left(int id) {
  for (int i = 0; i < N; ++i) {
    if (a[i] == -1) {
      a[i] = id;
      break;
    }
  }
}

void sit_a() {
  sit_from_left(0);
}

void sit_b() {
  bool sat = false;;
  for (int i = N - 1; i >= 0; --i) {
    if (a[i] != -1) {
      continue;
    }
    if (i > 0 && a[i - 1] == 0) {
      continue;
    }
    if (i < N - 1 && a[i + 1] == 0) {
      continue;
    }
    a[i] = 1;
    sat = true;
    break;
  }

  if (sat) {
    return;
  }

  sit_from_left(1);
}

void sit_c() {
  bool sat = false;
  for (int i = 0; i < N; ++i) {
    if (a[i] == -1) {
      continue;
    }
    if (i < N - 1 && a[i + 1] == -1) {
      a[i + 1] = 2;
      sat = true;
      break;
    }
    if (i > 0 && a[i - 1] == -1) {
      a[i - 1] = 2;
      sat = true;
      break;
    }
  }

  if (sat) {
    return;
  }

  a[N / 2 + 1] = 2;
}

void sit_d() {
  int m = 0;
  int mi = 0;
  for (int i = 0; i < N; ++i) {
    if (a[i] != -1) {
      continue;
    }
    int l = N;
    int r = N;
    for (int j = i; j >= 0; --j) {
      if (a[j] != -1) {
        l = i - j;
        break;
      }
    }
    for (int j = i; j < N; ++j) {
      if (a[j] != -1) {
        r = j - i;
        break;
      }
    }
    if (m < min(l, r)) {
      m = min(l, r);
      mi = i;
    }
  }
  a[mi] = 3;
}

int main() {
  while (1) {
    cin >> N >> M;
    if (!N && !M) {
      break;
    }
    fill(a, a + N, -1);
    for (int i = 0; i < M; ++i) {
      string s;
      cin >> s;
      if (s == "A") {
        sit_a();
      } else if (s == "B") {
        sit_b();
      } else if (s == "C") {
        sit_c();
      } else if (s == "D") {
        sit_d();
      }
    }
    for (int i = 0; i < N; ++i) {
      if (a[i] < 0) {
        cout << (char)'#';
      } else {
        cout << (char)('A' + a[i]);
      }
    }
    cout << endl;
  }
  return 0;
}

C++: // 2011/07/21 Tazoe

#include <iostream>
#include <climits>
using namespace std;

int main()
{
	while(true){
		int n, m;
		cin >> n >> m;
		if(n==0&&m==0)
			break;

		char C[100];
		for(int i=0; i<n; i++)
			C[i] = '#';

		for(int i=0; i<m; i++){
			char a;
			cin >> a;

			bool flg;
			switch(a){
				case 'A':
					for(int j=0; j<n; j++)
						if(C[j]=='#'){
							C[j] = a;
							break;
						}
					break;
				case 'B':
					flg = false;
					for(int j=n-1; j>=0; j--)
						if(C[j]=='#'&&(j==0||C[j-1]!='A')&&(j==n-1||C[j+1]!='A')){
							C[j] = a;
							flg = true;
							break;
						}
					if(!flg)
						for(int j=0; j<n; j++)
							if(C[j]=='#'){
								C[j] = a;
								break;
							}
					break;
				case 'C':
					if(i==0){
						C[n/2] = a;
						break;
					}
					for(int j=0; j<n; j++)
						if(C[j]!='#'&&j!=n-1&&C[j+1]=='#'){
							C[j+1] = a;
							break;
						}
						else if(C[j]!='#'&&j!=0&&C[j-1]=='#'){
							C[j-1] = a;
							break;
						}
					break;
				case 'D':
					if(i==0){
						C[0] = a;
						break;
					}
					int D[100];
					for(int j=0; j<n; j++){
						if(C[j]!='#'){
							D[j] = 0;
							continue;
						}
						int d = 0;
						flg = false;
						for(int k=j; k<n; k++, d++)
							if(C[k]!='#'){
								flg = true;
								break;
							}
						if(flg)
							D[j] = d;
						else
							D[j] = INT_MAX;
						d = 0;
						flg = false;
						for(int k=j; k>=0; k--, d++)
							if(C[k]!='#'){
								flg = true;
								break;
							}
						if(flg&&d<D[j])
							D[j] = d;
					}
					int maxD = -1;
					int maxI = -1;
					for(int j=0; j<n; j++)
						if(maxD<D[j]){
							maxD = D[j];
							maxI = j;
						}
					C[maxI] = a;				
					break;
			}
		}

		for(int i=0; i<n; i++)
			cout << C[i];
		cout << endl;
	}

	return 0;
}