fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define maxlen 1000000001
  5.  
  6. char expr[maxlen], bset[maxlen];
  7. int pos;
  8. typedef struct _node{
  9. int data;
  10. struct _node *left;
  11. struct _node *right;
  12. }Node;
  13.  
  14. int parseNum(){
  15. int res = 0;
  16. while('0' <= expr[pos] && expr[pos] <= '9'){
  17. res = res*10 + (expr[pos]-'0');
  18. pos++;
  19. }
  20. return res-1;
  21. }
  22.  
  23. Node* expr2ast(){
  24. if(expr[pos] == '\0') return NULL;
  25. Node* root = (Node*)malloc(sizeof(Node));
  26. root->data = parseNum();
  27. // printf("parsed: %d, pos = %d\n", root->data, pos);
  28. if(expr[pos] == '?'){
  29. pos++;
  30. root->left = expr2ast();
  31. root->right= expr2ast();
  32. }else if(expr[pos] == ':'){
  33. pos++;
  34. }
  35. return root;
  36. }
  37.  
  38. void preOrder(Node* root){
  39. if(root){
  40. printf("%d ", root->data);
  41. preOrder(root->left);
  42. preOrder(root->right);
  43. }
  44. }
  45.  
  46. void inOrder(Node* root){
  47. if(root){
  48. inOrder(root->left);
  49. printf("%d ", root->data);
  50. inOrder(root->right);
  51. }
  52. }
  53. int cclAST(Node* root){
  54. // printf("node: %d, value: %d\n", root->data, bset[root->data] - '0');
  55. if(root->left == NULL || root->right == NULL){
  56. // printf("finally: %d\n", bset[root->data] - '0');
  57. return bset[root->data] - '0';
  58. }
  59. if(bset[root->data] - '0'){
  60. return cclAST(root->left);
  61. }else{
  62. return cclAST(root->right);
  63. }
  64. }
  65.  
  66. signed main(signed argc, char* argv[]){
  67. scanf(" %s", expr);
  68. pos = 0;
  69. Node* ast = expr2ast();
  70. // preOrder(ast); printf("\n");
  71. // inOrder(ast); printf("\n");
  72. int T; scanf(" %d", &T);
  73. while(T--){
  74. scanf(" %s", bset);
  75. // printf("%c %d\n", bset[4], bset[4] - '0');
  76. printf("%d\n", cclAST(ast));
  77. }
  78. return 0;
  79. }
Success #stdin #stdout 0.01s 5288KB
stdin
1?2?3:4:5?6:7
4
1110000
0000001
1001000
0000110
stdout
1
1
1
1