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 <bits/stdc++.h>
using namespace std;

int main(){
    while(1){
        int n, m; cin>>n>>m;
        if(n == 0) return 0;

        string s = "";
        for(int i=0; i<n; i++){
            s += "#";
        }

        while(m--){
            char a; cin>>a;
            if(a == 'A'){
                for(int i=0; i<n; i++){
                    if(s[i] == '#'){
                        s[i] = 'A';
                        break;
                    }
                }
            }
            else if(a == 'B'){
                bool valid = false;
                for(int i=n-1; i>=0; i--){
                    if(s[i] != '#') continue;

                    if((i + 1 < n && s[i+1] == 'A') || (0 <= i - 1 && s[i-1] == 'A')) continue;
                
                    s[i] = 'B';
                    valid = true;
                    break;
                }

                if(valid) continue;

                for(int i=0; i<n; i++){
                    if(s[i] == '#'){
                        s[i] = 'B';
                        break;
                    }
                }
            }
            else if(a == 'C'){
                bool valid = false;
                for(int i=0; i<n; i++){
                    if(s[i] != '#'){
                        if(i + 1 < n && s[i+1] == '#'){
                            s[i+1] = 'C';
                            valid = true;
                            break;
                        }
                        
                        if(0 <= i - 1 && s[i-1] == '#'){
                            s[i-1] = 'C';
                            valid = true;
                            break;
                        }
                    }
                }

                if(valid) continue;

                s[n/2] = 'C';
            }
            else{
                int dist = 0;
                int id = -1;
                for(int i=0; i<n; i++){
                    if(s[i] != '#') continue;
                    int near = 1e9;
                    for(int j=0; j<n; j++){
                        if(i == j || s[j] == '#') continue;
                        
                        near = min(near, abs(i - j));
                    }

                    if(near == 1e9) continue;

                    if(dist < near){
                        dist = near;
                        id = i;
                    }
                }

                if(id != -1){
                    s[id] = 'D';
                }
                else{
                    s[0] = 'D';
                }
            }
        }

        cout << s << endl;
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second

int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int seki[101];

main(){
  int m,n;
  while(cin>>n>>m,n|m){
    string seat;
    rep(i,n)seat+='#';

    rep(i,m){
      //cout<<seat<<endl;
      char c;
      int pos=0;
      cin>>c;
      switch(c){
      case 'A':
	pos=0;
	while(seat[pos]!='#')++pos;
	seat[pos]='A';
	break;
      case 'B':
	pos=n-1;
	while(true){
	  if(pos<0)break;
	  if(seat[pos]!='#'){
	    --pos;
	    continue;
	  }
	  if(pos && seat[pos-1]=='A'){
	    --pos;
	    continue;
	  }
	  if(pos+1<n && seat[pos+1]=='A'){
	    --pos;
	    continue;
	  }
	  seat[pos]='B';
	  break;
	}
	if(pos==n){
	  pos=0;
	  while(seat[pos]!='#')++pos;
	  seat[pos]='B';
	}
	break;
      case 'C':
	if(i==0){
	  if(n%2)seat[(n+1)/2]='C';
	  else seat[n/2+1]='C';
	}else{
	  pos=0;
	  while(seat[pos]!='#')++pos;
	  seat[pos]='C';
	}
	break;
      case 'D':
	if(i==0){
	  seat[0]='D';
	  continue;
	}
	memset(seki,0,sizeof(0));
	int maxt=0,maxpos=0;

	if(seat[0]=='#'){
	  seki[0]=1;
	  maxt=1;
	  maxpos=1;
	}
	pos=1;
	while(pos<n){
	  if(seat[pos]=='#')seki[pos]=seki[pos-1]+1;
	  else seki[pos]=0;
	  if(seki[pos]>maxt){
	    maxt=seki[pos];
	    maxpos=pos;
	  }
	  ++pos;
	}
	//rep(j,n)cout<<seki[j]<<" ";
	//cout<<endl;
	//cout<<maxpos<<" "<<maxt<<endl;
	if(maxpos==n-1)seat[n-1]='D';
	else if(maxt%2)seat[maxpos-maxt/2]='D';
	else seat[maxpos-maxt/2]='D';
	break;
      }
    }
    cout<<seat<<endl;
  }
}