/*
    0
  1   2
 34   56
789A BCDE
*/
/*
rule
if == then left
else right
if ==0 then return
if both child== then output

null should act as null
quine should return itself
*/

/*preprocessor*/
#include <iostream>
#include <fstream>

using namespace std;

/*proto*/
const char * load(const char*);
int left(int );
int right(int );
int len(const char * );
char get(int , const char * );

bool sibling(int ptr,const char * code){
  if (get(left(ptr),code)==get(right(ptr),code)){
    cout << get(ptr,code) << endl;
    return true;
  }
  return false;
}

int go(char c, int ptr, const char * code){
  if (get(ptr,code)==0)
    return 0;
  else{
    if (sibling(ptr,code))
      return 0;
    else if(get(ptr,code)==c){
      if (sibling(left(ptr),code))
        return 0;
      return left(ptr);
    }
    else{
      return go(c,right(ptr),code);
    }
  }
}

/*main*/
int main(int argc, char* argv[]){
  /*var*/
  const char * code = argc>1?load(argv[1]):"";
  int ptr=0;
  char c;
  cout << '[' << code << ']' << endl;
  /*language*/
  while(true){
    cin >> c;
    ptr=go(c,ptr,code);
  }
  /*end*/
  return 0;
}

/*function*/
const char * load(const char * filename){
  if(filename==NULL)
    return "";
  int ptr = 0;
  char * code = new char[1024];
  char c;
  ifstream myfile;
  myfile.open(filename);
  while(!myfile.eof()){
    myfile >> c;
    if (c!='\n' && c!='\r')
      code[ptr++]=c;
  }
  code[--ptr]=0;
  myfile.close();
  return code;
}
int left(int i){
  return ((i+1)<<1)-1;
}
int right(int i){
  return (i+1)<<1;
}
int len(const char * cod){
  int n=0;
  while(*(cod+n))
    n++;
  return n;
}
char get(int i, const char * cod){
  if (i >= len(cod))
    return 0;
  return *(cod+i);
}
