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[:len(chair)-1], ""))
	}
}

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<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

int main()
{
	int m, n;
	while (cin >> n >> m, n, m) {

		int used[110] = {0};
		int emp[110] = {0};

		for (int i = 0; i < m; i++) {
			char a; cin >> a;
			if (a == 'A') {
				for (int j = 0; j < n; j++) {
					if (used[j] == 0) {
						used[j] = 1;
						break;
					}
				}
			}
			else if (a == 'B') {
				bool flag = false;
				for (int j = n - 1; j >= 1; j--) {
					if (used[j] == 0) {
						if (used[j + 1] != 1 && used[j - 1] != 1) {
							flag = true;
							used[j] = 2;
							break;
						}
					}
				}
				if (!flag) {
					for (int j = 0; j < n; j++) {
						if (used[j] == 0) {
							used[j] = 2;
							break;
						}
					}
				}
			}
			else if (a == 'C') {
				bool flag = false;
				for (int j = 0; j < n; j++) {
					if (used[j]) {
						flag = true;
						break;
					}
				}
				if (!flag) {
					if (n % 2 == 0)used[n / 2 + 1] = 3;
					else used[(n + 1) / 2] = 3;
				}
				else {
					for (int j = 0; j < n; j++) {
						if (used[j]) {
							if ((j + 1 < n) && used[j + 1] == 0) {
								used[j + 1] = 3;
								break;
							}
							else {
								if (j - 1 >= 0 && used[j - 1] == 0) {
									used[j - 1] = 3;
									break;
								}
							}
						}
					}
				}
			}
			else if (a == 'D') {
				bool flag = false;
				for (int j = 0; j < n; j++) {
					if (used[j]) {
						flag = true;
						break;
					}
				}
				if (!flag) {
					used[0] = 4;
				}
				else {
					int cnt = 0, mx = 0, mx_ind = 0;
					for (int j = 0; j < n; j++) {
						if (used[j] == 0)cnt++;
						else cnt = 0;
						emp[j] = cnt;
						if (mx < cnt) {
							mx = cnt;
							mx_ind = j;
						}
					}
					used[mx_ind - (mx / 2)] = 4;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			if (used[i] == 1)cout << 'A';
			if (used[i] == 2)cout << 'B';
			if (used[i] == 3)cout << 'C';
			if (used[i] == 4)cout << 'D';
			if (used[i] == 0)cout << '#';
		}
		cout << endl;
	}
	return 0;
}

C++: #include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int n, q; char c; bool flag, flag1, flag2;
int main() {
	while (cin >> n >> q, n) {
		string s(n, '#');
		for(int l = 0; l < q; l++) {
			cin >> c;
			switch (c) {
			case 'A':
				for (int i = 0; i < n; i++) {
					if (s[i] == '#') {
						s[i] = 'A'; break;
					}
				}
				break;
			case 'B':
				flag = false;
				for (int i = n - 1; i >= 0; i--) {
					if (s[i] != '#') continue;
					flag2 = true;
					for (int j = -1; j <= 1; j += 2) {
						if (0 <= i + j && i + j < n && s[i + j] == 'A') flag2 = false;
					}
					if (flag2) {
						s[i] = 'B'; flag = true; break;
					}
				}
				if (!flag) {
					for (int i = 0; i < n; i++) {
						if (s[i] == '#') {
							s[i] = 'B'; break;
						}
					}
				}
				break;
			case 'C':
				flag1 = false;	
				if (l == 0) {
					s[n / 2] = false; break;
				}
				for (int i = 0; i < n; i++) {
					for (int j = 1; j >= -1; j -= 2) {
						if (0 <= i + j && i + j < n && s[i + j] == '#') {
							s[i + j] = 'C'; flag1 = true; break;
						}
					}
					if (flag1) break;
				}
				break;
			case 'D':
				int ptr = -1, ret = -1;
				for (int i = 0; i < n; i++) {
					int res = 999999999;
					for (int j = 0; j < n; j++) {
						if (s[j] != '#') res = min(res, abs(i - j));
					}
					if (ret < res) ptr = i, ret = res;
				}
				s[ptr] = 'D';
				break;
			}
		}
		cout << s << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m;
  while(cin>>n>>m,n){
    string s;
    for(int i=0;i<n;i++)s+='#';
    while(m--){
      string p;
      cin>>p;
      if(p=="A"){
        for(int i=0;i<n;i++)
          if(s[i]=='#'){
            s[i]='A';
            break;
          }
      }
      if(p=="B"){
        int pp=0;
        for(int i=n-1;i>=0;i--){
          if(i==n-1){if(s[i-1]!='A'&&s[i]=='#'){pp++,s[i]='B';break;}}
          else if(!i){if(s[i+1]!='A'&&s[i]=='#'){pp++,s[i]='B';break;}}
          else {if(s[i-1]!='A'&&s[i]=='#'&&s[i+1]!='A'){pp++,s[i]='B';break;}}
        }
        if(!pp)for(int i=0;i<n;i++)
             if(s[i]=='#'){
               s[i]='B';
               break;
            }
      }
      if(p=="C"){
        int pp=0;
        for(int i=0;i<n;i++)
          if(s[i]!='#'){
            
            if(i!=n-1){if(s[i+1]=='#'){s[i+1]='C';pp++;break;}}
            if(i){if(s[i-1]=='#'){s[i-1]='C';pp++;break;}}
          }
        if(!pp){
          if(n%2)s[(n+1)/2-1]='C';
          else s[n/2]='C';
        }
      }
      else if(p=="D"){
        int d[n]={},p=0,sp=0;
        for(int i=0;i<n;i++){

          int p1=0,p2=0;
          if(i==n-1)p1=1000;
          if(i==0)p2=1000;
          for(int j=i;j<n;j++)
            if(s[j]=='#')p1++;
            else break;
          for(int j=i;j>=0;j--)
            if(s[j]=='#')p2++;
            else break;
          p1=min(p1,p2);
          p=max(p,p1);
          d[i]=p1;//cout<<d[i];
        }//cout<<endl;
        for(int i=0;i<n;i++)if(s[i]!='#')sp++;
        if(!sp)s[0]='D';
        else for(int i=0;i<n;i++)
          if(d[i]==p){
            s[i]='D';
            break;
          }
      }

    }
    cout<<s<<endl;
  }
}