考虑对于任意两个数q1[i]和q1[j]来说,它们不能压入同一个栈中的充要条件是存在一个k,使得i<j<k且q1[k]<q1[i]<q1[j] 我们对所有的数对(i,j)满足1<=i<j<=n,检查是否存在i<j<k满足p1[k]<p1[i]<p1[j].如果存在,那么在点i和点j之间连一条无向边,表示p1[i]和p1[j]不能压入同一个栈,这就是二分图。只要判断这是不是二分图,如果是,模拟输出即可

program twostack;
var
  n,i:integer;
  s:array[1..2,0..1000] of integer;
  a,color:array[1..1000] of integer;
  b:array[1..1001] of integer;
  g:array[1..1000,0..1000] of integer;
procedure build;
var
  i,j,min:integer;
begin
  min:=maxint;
  for i:=n downto 1 do
  begin
    if a[i]<min then min:=a[i];
    b[i]:=min;
  end;
  b[n+1]:=n+1;
  for i:=1 to n-1 do
    for j:=i+1 to n do
      if (b[j+1]<a[i]) and (a[i]<a[j]) then
      begin
        inc(g[i,0]);
        g[i,g[i,0]]:=j;
        inc(g[j,0]);
        g[j,g[j,0]]:=i;
      end;
end;
procedure dfs(i:integer);
var
  j:integer;
begin
  for j:=1 to g[i,0] do
    if color[g[i,j]]=0 then
    begin
      color[g[i,j]]:=3-color[i];
      dfs(g[i,j]);
    end
    else if color[g[i,j]]=color[i] then
    begin
      writeln('0');
      close(input);
      close(output);
      halt;
    end;
end;
procedure print;
var
  i,now,c:integer;
begin
  now:=1;
  for i:=1 to n do
  begin
    if color[i]=1 then write('a ')
      else write('c ');
    c:=color[i];
    inc(s[c,0]);
    s[c,s[c,0]]:=a[i];
    while (s[1,s[1,0]]=now) or (s[2,s[2,0]]=now) do
    begin
      if s[1,s[1,0]]=now then
      begin
        if now<>n then write('b ')
          else writeln('b');
        dec(s[1,0]);
      end
      else
      begin
        if now<>n then write('d ')
          else writeln('d');
        dec(s[2,0]);
      end;
      inc(now);
    end;
  end;
end;
begin
  assign(input,'twostack.in');
  reset(input);
  assign(output,'twostack.out');
  rewrite(output);
  fillchar(a,sizeof(a),0);
  fillchar(b,sizeof(b),0);
  fillchar(g,sizeof(g),0);
  fillchar(s,sizeof(s),0);
  fillchar(color,sizeof(color),0);
  readln(n);
  for i:=1 to n do
    read(a[i]);
  readln;
  build;
  for i:=1 to n do
    if color[i]=0 then
    begin
      color[i]:=1;
      dfs(i);
    end;
  print;
  close(input);
  close(output);
end.