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<bits/stdc++.h>
using namespace std;
int main()
{
	int N,M;
	while(true){
		scanf("%d%d",&N,&M);
		if(N==0&&M==0)return 0;
		int seats[10009];
		for(int i=0;i<10009;i++){
			seats[i]=0;
		}
		
		for(int i=0;i<M;i++){
			char S[10009];
			scanf("%s",&S);
			if(S[0]=='A'){
				
				for(int j=0;j<N;j++){
					if(seats[j]==0){
						seats[j]=1;
						break;
					}
				}
			}
			else if(S[0]=='B'){
				bool F=true;
				for(int j=N-1;j>=0;j--){
					if(F&&(j==0||seats[j-1]!=1)&&(j==N-1||seats[j+1]!=1)&&seats[j]==0){
						seats[j]=2;F=false;
					}
				}
				if(F){
					for(int j=0;j<N;j++){
						if(seats[j]==0){
							seats[j]=2;
							break;
						}
					}
				}
			}
			else if(S[0]=='C'){
				bool F=true;
				for(int j=0;j<N;j++){
					if(seats[j]!=0){
						if(j!=N-1&&seats[j+1]==0){
							seats[j+1]=3;F=false;
						}
						else if(j!=0&&seats[j-1]==0){
							seats[j-1]=3;F=false;
						}
					}
					if(!F)break;
				}
				if(F)seats[N/2]=3;
			}
			else{
				int maxd=-1,maxi;
				bool F=true;
				for(int i=0;i<N;i++){
					if(seats[i]!=0)F=false;
				}
				if(F){
					seats[0]=4;
					continue;
				}
				for(int i=0;i<N;i++){
					int d=0;
					if(seats[i]!=0)d=-114;
					int l=i;int r=i;
					while(l>=0&&seats[l]==0&&r<N&&seats[r]==0){
						d++;
						l--;
						r++;
					}
					if(maxd<d){
						maxd=d;
						maxi=i;
					}
					
				}
				seats[maxi]=4;
			}
			
		}
		for(int i=0;i<N;i++){
				if(seats[i]==0)printf("#");
				if(seats[i]==1)printf("A");
				if(seats[i]==2)printf("B");
				if(seats[i]==3)printf("C");
				if(seats[i]==4)printf("D");
			}
			printf("\n");
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int main(){
	int n,m;
	while(cin>>n>>m,n){
		char input,chair[100];
		rep(i,n)chair[i]='#';
		rep(i,m){
			bool stop=false;
			cin>>input;
			if(input=='A'){
				rep(j,n)if(chair[j]=='#'){
					chair[j]='A';
					break;
				}
			}else if(input=='B'){
				rep(j,n)if(chair[j]=='#'&&(j==0||chair[j-1]!='A')&&(j==n-1||chair[j+1]=='A')){
					chair[j]='B';
					stop=true;
					break;
				}
				if(!stop)rep(j,n)if(chair[j]=='#'){
					chair[j]='B';
					break;
				}
			}else if(input=='C'){
				if(i==0)chair[n/2+1]='C';
				else rep(j,n)if(chair[j]=='#'&&(j==0||chair[j-1]!='#')&&(j==n-1||chair[j+1]!='#')){
					chair[j]='C';
					stop=true;
					break;
				}
				if(!stop)rep(j,n)if(chair[j]=='#'){
					chair[j]='C';
					break;
				}
			}else{
				rep(j,n)if(chair[j]=='#'&&(j==0||chair[j-1]=='#')&&(j==n-1||chair[j+1]=='#')){
					chair[j]='D';
					stop=true;
					break;
				}
				if(!stop)rep(j,n)if(chair[j]=='#'){
					chair[j]='D';
					break;
				}
			}
		}
		rep(i,n)cout<<chair[i];
		cout<<endl;
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>

using namespace std;

void PersonA(string&);
void PersonB(string&);
void PersonC(string&);
void PersonD(string&);
bool UsingSeat(const char&);

int main(){
  int i, n, m;
  string c;

  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    string chair(n, '#');

    for(i=0; i<m; ++i){
      cin >> c;
      if(c == "A") PersonA(chair);
      else if(c == "B") PersonB(chair);
      else if(c == "C") PersonC(chair);
      else if(c == "D") PersonD(chair);
    }
    cout << chair << endl;
  }
  return 0;
}

void PersonA(string& s){
  string::iterator i;
  i = find(s.begin(), s.end(), '#');
  *i = 'A';
}

void PersonB(string& s){
  string::reverse_iterator i, j;
  string::iterator k;
  for(j=s.rbegin(); i!=s.rend(); j=i+1){
    i = find(j, s.rend(), '#');
    if((i == s.rbegin() && *(i+1) != 'A') ||
       (i == s.rend()-1 && *(i-1) != 'A') ||
       (*(i-1) != 'A' && *(i+1) != 'A')){
      *i = 'B';
      return;
    }
  }
  k = find(s.begin(), s.end(), '#');
  *k = 'B';
}

void PersonC(string& s){
  string::iterator i, j;
  i = find_if(s.begin(), s.end(), UsingSeat);
  if(i == s.end()){
    s[s.size()/2] = 'C';
    return;
  }
  while(i != s.end()){
    j = find_if(i, s.end(), UsingSeat);
    if(*(j+1) == '#'){
      *(j+1) = 'C';
      return;
    }
    if(*(j-1) == '#'){
      *(j-1) = 'C';
      return;
    }
    ++i;
  }
}

void PersonD(string& s){
  string::iterator i, j, k;
  i = find_if(s.begin(), s.end(), UsingSeat);
  if(i == s.end()){
    s[0] = 'D';
    return;
  }
  int empty, _empty;
  empty = i - s.begin();
  k = s.begin();
  ++i;
  while(1){
    j = find_if(i, s.end(), UsingSeat);
    if(j == s.end()) break;
    _empty = j - i;
    if(_empty%2 == 0 && _empty/2 > empty){
      empty = _empty/2;
      k = i + empty - 1;
    }
    else if(_empty%2 == 1 && _empty/2 + 1 > empty){
      empty = _empty/2 + 1;
      k = i + empty - 1;
    }
    i = j + 1;
  }
  _empty = s.end() - i;
  if(_empty > empty) k = s.end()-1;
  *k = 'D';
}

bool UsingSeat(const char& c){
  if(c == '#') return false;
  else return true;
}