Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct node_s {
  6.     int idx, pos, anc, pes;
  7. };
  8.  
  9. void preprocess (struct node_s ***T, char *S, int *P, int N) {
  10.     int i, j, k;
  11.     struct node_s **t = (*T);
  12.  
  13.     i = 0;
  14.     j = 0;
  15.     while (i < 2*N) {
  16.         if (S[i] == '(') {
  17.             struct node_s *n = t[j];
  18.             n->idx = j;
  19.             n->pos = i;
  20.             n->pes = P[j];
  21.             j++;
  22.         }
  23.         i++;
  24.     }
  25.  
  26.     t[0]->anc = -1;
  27.     for (i = 1; i < N; ++i) {
  28.         int count = -1;
  29.         for (j = t[i]->pos; (j > 0) && (count != 0); --j)
  30.             count += (S[j-1] == '(') ? 1 : -1;
  31.         for (k = 0; k < N; ++k)
  32.             if (t[k]->pos == j) {
  33.                 t[i]->anc = k;
  34.                 break;
  35.             }
  36.     }
  37. }
  38.  
  39. int find_node (struct node_s ***T, int v, int P) {
  40.     struct node_s **t = (*T);
  41.     struct node_s *cur = t[v];
  42.     struct node_s *anc = t[cur->anc];
  43.  
  44.     while ((anc->anc != -1) && (anc->pes >= P)) {
  45.         cur = t[cur->anc];
  46.         anc = t[cur->anc];
  47.     }
  48.  
  49.     if ((anc->anc == -1) && (anc->pes >= P))
  50.         return anc->idx;
  51.  
  52.     return cur->idx;
  53. }
  54.  
  55. int main(int argc, char *argv) {
  56.     int i, N;
  57.     char *S, *W;
  58.     int *P;
  59.  
  60.     int Q;
  61.     char **queries;
  62.  
  63.     char *line = NULL;
  64.     size_t size;
  65.     int line_number = 1;
  66.  
  67.     char *seps = " ";
  68.     char *token;
  69.  
  70.     while (getline(&line, &size, stdin) > 0) {
  71.         line[strcspn(line, "\n")] = 0;
  72.  
  73.         switch (line_number) {
  74.         case 1:
  75.             N = atoi(line);
  76.             break;
  77.         case 2:
  78.             S = malloc(sizeof(char) * N);
  79.             strcpy(S, line);
  80.             break;
  81.         case 3:
  82.             i = 0;
  83.             token = strtok(line, seps);
  84.             P = malloc(sizeof(int) * N);
  85.             while (token != NULL && i < N) {
  86.                 int var;
  87.                 sscanf(token, "%d", &var);
  88.                 P[i++] = var;
  89.                 token = strtok(NULL, seps);
  90.             }
  91.             break;
  92.         case 4:
  93.             Q = atoi(line);
  94.             queries = malloc(sizeof(char *) * Q);
  95.             break;
  96.         default:
  97.             queries[line_number-5] = malloc(sizeof(char) * 3);
  98.             strcpy(queries[line_number-5], line);
  99.             break;
  100.         }
  101.  
  102.         line_number++;
  103.     }
  104.  
  105.     struct node_s **t = malloc(sizeof(struct node_s*) * N);
  106.     for (i = 0; i < N; ++i)
  107.         t[i] = malloc(sizeof(struct node_s));
  108.  
  109.     preprocess(&t, S, P, N);
  110.  
  111.     printf("%d\n", Q);
  112.     for (i = 0; i < Q; i++) {
  113.         int j = 0;
  114.         int params[2];
  115.         token = strtok(queries[i], seps);
  116.         while (token != NULL && j < 2) {
  117.             int var;
  118.             sscanf(token, "%d", &var);
  119.             params[j++] = var;
  120.             token = strtok(NULL, seps);
  121.         }
  122.         int result = find_node(&t, params[0], params[1]);
  123.         printf("%d\n", result);
  124.     }
  125.  
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement