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
		}
		chairs := make([]string, n)
		for i := range chairs {
			chairs[i] = "#"
		}
		sit := func(who string) {
			if who == "A" {
				chairs[getStringIndex(chairs, "#")] = "A" // A国の人は左から見て行って空いている席を見つけ次第座る
				// log.Println(chairs)
			} else if who == "B" {
				f := true
				for i := n-1; i > -1; i-- {
					// B国の人はA国人の隣以外で、右端から空いている椅子に座る
					if chairs[i] == "#" && (i == 0 || chairs[i-1] != "A") && (i == n-1 || chairs[i+1] != "A") {
								chairs[i] = "B"
								f = false
								break
					}
				}
				// A国人の隣しか空いていない場合、我慢して左端から空いている椅子に座る
				if f {
					chairs[getStringIndex(chairs, "#")] = "B"
				}
				// log.Println(chairs)
			} else if who == "C" { // C国人は人の隣に座りたい
				for i := 0; i < n; i++ {
					if chairs[i] != "#" {
						if i + 1 <= n-1 && chairs[i+1] == "#" {
							chairs[i+1] = "C"
							break
						} else if i - 1 >= 0 && chairs[i-1] == "#" {
							chairs[i-1] = "C"
							break
						}
					}
				}
				// log.Println(chairs)
			} else { // who == "D"
				// D国の人は人から離れて座りたい
				// どうしても誰かの隣に座らなければならない場合、その中で一番左側にある椅子に座る
				scores := make([]int, n)
				for i := range scores {
					scores[i] = INF
				}
				for i, v := range chairs {
					if v == "#" {
						scores[i]++
					}
				}
				for i := n-1; i > -1; i-- {
					tmp := 0
					if chairs[i] == "#" {
						tmp++
					} else {
						tmp = 0
					}
					scores[i] = min(scores[i], tmp)
				}
				chairs[getIntIndex(scores, getMax(scores))] = "D"
				// log.Println(chairs)
			}
		}
		scanner.Scan()
		if first := scanner.Text(); first == "A" || first == "D" {
			chairs[0] = first
		} else if first == "B" {
			chairs[len(chairs)-1] = "B"
		} else {
			chairs[n/2] = "C"
		}
		// log.Println(chairs)
		for i := 0; i < m-1; i++ {
			scanner.Scan()
			sit(scanner.Text())
		}
		fmt.Println(strings.Join(chairs, ""))
	}
}

func getStringIndex(chairs []string, who string) int {
	for i, v := range chairs {
		if v == who {
			return i
		}
	}
	return -1
}

func getIntIndex(scores []int, target int) int {
	for i, v := range scores {
		if v == target {
			return i
		}
	}
	return -1
}

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

func getMax(scores []int) int {
	tmp := -1
	for _, v := range scores {
		if tmp < v {
			tmp = v
		}
	}
	return tmp
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;int main(void){int i,n,m;while(cin>>n>>m&&n!=0&&m!=0){
vector<char> z(n,'#');for(int l=0;l<m;l++){char a;cin>>a;if(a=='A'){for(i=0;i<m;i++){if(z[i]=='#'){z[i]=a;break;}}}
else if(a=='B'){int su=0;for(i=n-1;i>=0;i--){if(z[i]!='#')continue;if(i>=1&&z[i-1]=='A')continue;
if(i<=n-2&&z[i+1]=='A')continue;z[i]=a;su=1;break;}if(!su){for(i=0;i<n;i++){if(z[i]=='#'){z[i]=a;break;}}}}
else if(a=='C'){int su=0;for(i=0;i<n;i++){if(z[i]=='#')continue;
if(i<=n-2&&z[i+1]=='#'){z[i+1]=a;su=1;break;}if(i>=1&&z[i-1]=='#'){z[i-1]=a;su=1;break;}}
if(!su){if(n%2==0){z[(n/2)] = a;}else{z[(n/2)]=a;}}}else if(a=='D'){int ef=1;
for(i=0;i<n;i++){if(z[i]!='#'){ef=0;break;}}if(ef){z[0]=a;continue;}
vector<int> C(n,0);for(i=0;i<n;i++){if(z[i]!='#'){int Y=1;while(i+Y<n&&z[i+Y]=='#'){
if(C[i+Y]==0)C[i+Y] = Y;else C[i+Y]=min(Y,C[i+Y]);Y++;}}}for(i=n-1;i>=0;i--){if(z[i]!='#'){int Y=1;
while(i-Y>=0&&z[i-Y]=='#'){if(C[i-Y]==0)C[i-Y]=Y;else C[i-Y]=min(Y,C[i-Y]);Y++;}}}int su=0,mn,mv=0;for(i=0;i<n;i++){if(C[i]>mv){mn=i;mv=C[i];}}if(mv>1){z[mn]=a;su=1;}if(!su){for(i=0;i<n;i++){if(z[i]=='#'){z[i]=a;break;}}}}}for(i=0;i<n;i++)cout<<z[i];cout<<endl;}return 0;}

C++: #define _USE_MATH_DEFINES
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<climits>
using namespace std;

int N, M;
char C;
char chairs[102];

void PutA(){
	for(int i = 1; i <= N; i++){
		if(chairs[i] == '#'){
			chairs[i] = 'A';
			return;
		}
	}
}

void PutB(){
	for(int i = N; i >= 1; i--){
		if(chairs[i+1] != 'A' && chairs[i] == '#' && chairs[i-1] != 'A'){
			chairs[i] = 'B';
			return;
		}
	}
	for(int i = 1; i <= N; i++){
		if(chairs[i] == '#'){
			chairs[i] = 'B';
			return;
		}
	}
}

void PutC(){
	for(int i = 1; i <= N; i++){
		if(chairs[i] != '#'){
			if(chairs[i+1] == '#'){
				chairs[i+1] = 'C';
				return;
			}
			if(chairs[i-1] == '#'){
				chairs[i-1] = 'C';
				return;
			}
		}
	}
	if(N&1) chairs[N/2 + 1] = 'C';
	chairs[ (N+1)/2 ] = 'C';
}

void PutD(){
	int l[102], r[102];
	l[0] = INT_MAX/2; r[N+1] = INT_MAX/2;
	for(int i = 1; i <= N; i++) l[i] = (chairs[i] == '#') ? l[i-1] + 1 : 0;
	for(int i = N; i >= 1; i--) r[i] = (chairs[i] == '#') ? r[i+1] + 1 : 0;

	int ma_idx, ma_d = -1;
	for(int i = 1; i <= N; i++){
		int d = min(l[i], r[i]);
		if(d >= INT_MAX/2) d = INT_MAX;
		if(ma_d < d){
			ma_idx = i; ma_d = d;
		}
	}
	chairs[ma_idx] = 'D';
}

int main(){
	void (*put[4])() = {PutA, PutB, PutC, PutD};

	while(cin >> N >> M, N || M){
		fill_n(chairs, N+2, '#');
		chairs[0] = chairs[N+1] = '.';
		for(int i = 0; i < M; i++){
			cin >> C;
			put[C - 'A']();
		}
		for(int i = 1; i <= N; i++) printf("%c", chairs[i]); puts("");
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main()
{
  int n,m;
  char c,isu[200];
 
  while(1){
    cin>>n>>m;
    if(n+m==0)break;
    for(int i=0;i<200;i++)isu[i]='#';

    for(int k=0;k<m;k++){
      cin>>c;
      if(c=='A'){
	for(int i=0;i<n;i++){
	  if(isu[i]=='#'){
	    isu[i]='A';
	    break;
	  }
	}
      }

      if(c=='B'){
	bool flag=false;
	for(int i=n-1;i>=1;i--){
	  if(isu[i-1]!='A' && isu[i]=='#'){
	    isu[i]='B';
	    flag=true;
	    break;
	  }
	}
	if(flag)continue;
	for(int i=0;i<n;i++){
	  if(isu[i]=='#'){
	    isu[i]='B';
	    break;
	  }
	}
      }
      if(c=='C'){
	if(k==0){
	  if(n%2==0){
	    isu[n/2+1-1]='C';
	  }
	  else isu[(n+1)/2-1]='C';
	  continue;
	}
        for(int i = 0;i<n-1;i++){
	  if(isu[i]!='#'){
	    if(isu[i+1]=='#'){
	      isu[i+1]='C';
	      break;
	    }
            if(i!=0 && isu[i-1]=='#'){
	      isu[i-1]='C';
	      break;
	    }
	  }
	}
      }
      if(c=='D') {
	if(isu[0] == '#'){
	  isu[0]='D';
	  continue;
	}
	if(isu[n-1] == '#'){
	  isu[n-1]='D';
	  continue;
	}
	int ma=0,index;
	for(int i = 0;i < n;i++){
	  if(isu[i] == '#'){
	    for(int j=i+1;j<n;j++){
	      if(isu[j]!='#' ||  j==n-1){
		if(ma<j-i-1){
		  ma=j-i-1;
		  index=i;
		}
		i=j;
		break;
	      }
	    }
	  }
	}
	isu[index+ma/2]='D';
      }
    }
    for(int i=0;i<n;i++)cout<<isu[i];
    cout<<endl;
  }
  return 0;
}