結果

問題 No.430 文字列検索
ユーザー akakimidoriakakimidori
提出日時 2019-08-13 15:34:23
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 1,026 bytes
コンパイル時間 335 ms
コンパイル使用メモリ 30,120 KB
実行使用メモリ 6,432 KB
最終ジャッジ日時 2023-10-19 17:09:10
合計ジャッジ時間 1,210 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 6 ms
6,432 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 AC 3 ms
4,384 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 8 ms
4,928 KB
testcase_12 AC 8 ms
6,432 KB
testcase_13 AC 8 ms
6,432 KB
testcase_14 AC 7 ms
6,432 KB
testcase_15 AC 5 ms
4,348 KB
testcase_16 AC 5 ms
4,348 KB
testcase_17 AC 5 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>
#include<string.h>

typedef int32_t i32;

#define ALLOC(size,type) ((type*)calloc((size),sizeof(type)))

typedef struct node {
  i32 cnt;
  struct node *next[26];
} node;

node *NODE_ARRAY = NULL;
node* add(node *t, char *s) {
  if (t == NULL) {
    t = NODE_ARRAY++;
  }
  if (*s == '\0') {
    t->cnt++;
  } else {
    i32 k = *s - 'A';
    t->next[k] = add (t->next[k], s + 1);
  }
  return t;
}

void run (void) {
  char *s = ALLOC (50000 + 1, char);
  scanf ("%s", s);
  i32 m;
  scanf ("%" SCNi32, &m);
  NODE_ARRAY = ALLOC(m * 10 + 1, node);
  node *r = NULL;
  while (m--) {
    char c[11];
    scanf ("%s", c);
    r = add (r, c);
  }
  i32 n = strlen (s);
  i32 ans = 0;
  for (i32 i = 0; i < n; ++i) {
    node *p = r;
    for (i32 j = 0; j < 10 && j < n - i; ++j) {
      p = p->next[s[i + j] - 'A'];
      if (p == NULL) break;
      ans += p->cnt;
    }
  }
  printf ("%" PRIi32 "\n", ans);
}

int main (void) {
  run();
  return 0;
}
0