http://acm.pku.edu.cn/JudgeOnline/problem?id=1105

http://acm.pku.edu.cn/JudgeOnline/images/1105/1105_1.gif

Код:
#include <stdio.h>
#define MAX_DEPTH 12
int main() {
  int n, m;     
  int VO[MAX_DEPTH];                   
  char VarValues[MAX_DEPTH + 2];       
  char TermNodes[(1 << MAX_DEPTH) + 2];
  int i, j, tmp;
  int cnt = 1;
  while (1) {
    scanf(" %d\n", &n); 
    if (!n) break;  
    printf("S-Tree #%d:\n", cnt++);
    for (i = 0; i < n; i++) { 
      scanf(" x%d", &tmp);
      VO[i] = tmp - 1;
    }
    scanf("%s", TermNodes);
    scanf(" %d\n", &m); 
    for (i = 0; i < m; i++) {
      int pos = 0;
      scanf("%s", VarValues);
      for (j = 0; j < n; j++) {
	if (VarValues[VO[j]] == '1') pos |= (1 << (n - VO[j] - 1));
      }
      printf("%c", TermNodes[pos]);  /* Output the results */
    }
    printf("\n\n");
  }
  return 0;
}