Exemplo n.º 1
0
int
main(int argc, char **argv)
{
	int i, len, tests;
	scanf("%d", &tests);
	while(tests--) {
		scanf("%d", &n);
		max = INF;
		for(i=0; i<n; i++) {
			scanf("%s", str[i]);
			len = strlen(str[i]);
			if(len < max) {
				max = len;
				idx = i;
			}
		}
		printf("%d\n", find_substring());
	}
	return 0;
}
Exemplo n.º 2
0
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
{
    struct instance *o = vo;
    o->i = i;
    
    // read arguments
    NCDValRef delimiter_arg;
    NCDValRef input_arg;
    NCDValRef limit_arg = NCDVal_NewInvalid();
    if (!NCDVal_ListRead(params->args, 2, &delimiter_arg, &input_arg) && !NCDVal_ListRead(params->args, 3, &delimiter_arg, &input_arg, &limit_arg)) {
        ModuleLog(i, BLOG_ERROR, "wrong arity");
        goto fail0;
    }
    if (!NCDVal_IsString(delimiter_arg) || !NCDVal_IsString(input_arg) || (!NCDVal_IsInvalid(limit_arg) && !NCDVal_IsString(limit_arg))) {
        ModuleLog(i, BLOG_ERROR, "wrong type");
        goto fail0;
    }
    
    size_t limit = SIZE_MAX;
    if (!NCDVal_IsInvalid(limit_arg)) {
        uintmax_t n;
        if (!ncd_read_uintmax(limit_arg, &n) || n == 0) {
            ModuleLog(i, BLOG_ERROR, "bad limit argument");
            goto fail0;
        }
        n--;
        limit = (n <= SIZE_MAX ? n : SIZE_MAX);
    }
    
    const char *del_data = NCDVal_StringData(delimiter_arg);
    size_t del_len = NCDVal_StringLength(delimiter_arg);
    
    if (del_len == 0) {
        ModuleLog(i, BLOG_ERROR, "delimiter must be nonempty");
        goto fail0;
    }
    
    size_t *table = BAllocArray(del_len, sizeof(table[0]));
    if (!table) {
        ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
        goto fail0;
    }
    
    build_substring_backtrack_table(del_data, del_len, table);
    
    if (!ExpArray_init(&o->arr, sizeof(struct substring), 8)) {
        ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
        goto fail1;
    }
    o->num = 0;
    
    const char *data = NCDVal_StringData(input_arg);
    size_t len = NCDVal_StringLength(input_arg);
    
    while (1) {
        size_t start;
        int is_end = 0;
        if (limit == 0 || !find_substring(data, len, del_data, del_len, table, &start)) {
            start = len;
            is_end = 1;
        }
        
        if (!ExpArray_resize(&o->arr, o->num + 1)) {
            ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
            goto fail2;
        }
        
        struct substring *elem = &((struct substring *)o->arr.v)[o->num];
        
        if (!(elem->data = BAlloc(start))) {
            ModuleLog(i, BLOG_ERROR, "BAlloc failed");
            goto fail2;
        }
        
        memcpy(elem->data, data, start);
        elem->len = start;
        o->num++;
        
        if (is_end) {
            break;
        }
        
        data += start + del_len;
        len -= start + del_len;
        limit--;
    }
    
    BFree(table);
    
    // signal up
    NCDModuleInst_Backend_Up(i);
    return;

fail2:
    while (o->num-- > 0) {
        BFree(((struct substring *)o->arr.v)[o->num].data);
    }
    free(o->arr.v);
fail1:
    BFree(table);
fail0:
    NCDModuleInst_Backend_DeadError(i);
}
Exemplo n.º 3
0
int main(int argc,char **argv)
{
  int curr_arg;
  bool bVerbose;
  FILE *fptr0;
  int filename_len;
  FILE *fptr;
  int line_len;
  int line_no;
  static int dbg_line_no;
  int ix;
  int button_seat;
  int button_seat_ix;
  int player_found;
  int player_seat_ix;
  int seat_count;
  int big_blinds;
  int small_blinds;
  int others;
  int saw_flop;
  int saw_summary;
  int player_folded_before_flop;
  int total_hands;
  int big_blind_flops_seen;
  int small_blind_flops_seen;
  int other_flops_seen;
  int total_flops_seen;
  static int dbg_hand;
  int dbg;
  double flop_pct;
  int found;
  int num_folded;
  int num_mucked;
  int player_folds_str_len;

  if ((argc < 3) || (argc > 4)) {
    printf(usage);
    return 1;
  }

  bVerbose = false;

  for (curr_arg = 1; curr_arg < argc; curr_arg++) {
    if (!strcmp(argv[curr_arg],"-verbose")) {
      bVerbose = true;
      getcwd(save_dir,_MAX_PATH);
    }
    else
      break;
  }

  if (argc - curr_arg != 2) {
    printf(usage);
    return 2;
  }

  sprintf(player_folds_str,"%s: folds ",argv[curr_arg]);
  player_folds_str_len = strlen(player_folds_str);

  if ((fptr0 = fopen(argv[curr_arg+1],"r")) == NULL) {
    printf(couldnt_open,argv[curr_arg+1]);
    return 2;
  }

  big_blinds = 0;
  small_blinds = 0;
  others = 0;

  total_hands = 0;
  big_blind_flops_seen = 0;
  small_blind_flops_seen = 0;
  other_flops_seen = 0;
  num_folded = 0;
  num_mucked = 0;

  for ( ; ; ) {
    GetLine(fptr0,filename,&filename_len,MAX_FILENAME_LEN);

    if (feof(fptr0))
      break;

    total_hands++;

    if (total_hands == dbg_hand)
      dbg = 1;

    if ((fptr = fopen(filename,"r")) == NULL) {
      printf(couldnt_open,filename);
      continue;
    }

    line_no = 0;
    seat_count = 0;
    player_seat_ix = -1;
    saw_flop = 0;
    saw_summary = 0;
    player_folded_before_flop = 0;

    for ( ; ; ) {
      GetLine(fptr,line,&line_len,MAX_LINE_LEN);

      if (feof(fptr))
        break;

      line_no++;

      ix = 0;

      player_found = find_substring(line,&ix,argv[curr_arg],true,false);

      if (line_no == dbg_line_no)
        dbg = 1;

      if (line_no == 2) {
        ix = 0;

        if (find_substring(line,&ix,is_the_button,true,false)) {
          line[ix] = 0;
          sscanf(&line[ix-1],"%d",&button_seat);
        }
        else {
          printf("  %s: couldn't find the button seat\n",filename);
          continue;
        }
      }
      else if ((line_no >= 3) && (line_no <= 11) && !strncmp(line,seat_str,SEAT_STR_LEN)) {
        line[6] = 0;
        sscanf(&line[5],"%d",&seats[seat_count]);

        if (player_found)
          player_seat_ix = seat_count;

        if (seats[seat_count] == button_seat)
          button_seat_ix = seat_count;

        seat_count++;
      }
      else if (!strncmp(line,player_folds_str,player_folds_str_len)) {
        if (!saw_flop)
          player_folded_before_flop = 1;
      }
      else if (!strncmp(line,flop_str,FLOP_STR_LEN))
        saw_flop = 1;
      else if (!strncmp(line,summary_str,SUMMARY_STR_LEN))
        saw_summary = 1;
    }

    fclose(fptr);

    if (player_seat_ix == -1) {
      printf("  %s: couldn't find the player\n",filename);
      continue;
    }

    if (seat_count == 2) {
      if (button_seat_ix == player_seat_ix) {
        small_blinds++;

        if (!player_folded_before_flop)
          small_blind_flops_seen++;
      }
      else {
        big_blinds++;

        if (!player_folded_before_flop)
          big_blind_flops_seen++;
      }

      continue;
    }

    if (button_seat_ix == player_seat_ix) {
      others++;

      if (!player_folded_before_flop)
        other_flops_seen++;

      continue;
    }

    if (player_seat_ix < button_seat_ix)
      player_seat_ix += seat_count;

    if (player_seat_ix - button_seat_ix == 2) {
      big_blinds++;

      if (!player_folded_before_flop)
        big_blind_flops_seen++;
    }
    else if (player_seat_ix - button_seat_ix == 1) {
      small_blinds++;

      if (!player_folded_before_flop)
        small_blind_flops_seen++;
    }
    else {
      others++;

      if (!player_folded_before_flop)
        other_flops_seen++;
    }
  }

  fclose(fptr0);

  total_flops_seen = big_blind_flops_seen + small_blind_flops_seen + other_flops_seen;

  if (!total_hands)
    flop_pct = (double)0;
  else
    flop_pct = (double)total_flops_seen / (double)total_hands;

  printf("%8.6lf ",flop_pct);
  printf("(%d %d)",
    total_flops_seen,total_hands);

  if (!bVerbose)
    putchar(0x0a);
  else
    printf(" %s\n",save_dir);

  return 0;
}