Untitled
unknown
plain_text
10 months ago
1.3 kB
8
Indexable
#define MAX_NODE 350000 struct Node{ int cnt, child[26]; } nodes[MAX_NODE]; int nodeNum; int newNode(){ nodes[nodeNum].cnt = 0; for (int i = 0; i < 26; ++i) nodes[nodeNum].child[i] = -1; return nodeNum++; } int calc(char *str, int id, int command){ int count = 0; while (*str != '\0') { if (*str == '?') { for(int i = 0; i < 26; ++i) if (nodes[id].child[i] != -1) count += calc(str + 1, nodes[id].child[i], command); } else if (nodes[id].child[*str - 'a'] == -1) { return count; } else { id = nodes[id].child[*str - 'a']; } str++; } count += nodes[id].cnt; nodes[id].cnt *= command; return count; } void init() { nodeNum = 0; newNode(); } int add(char str[]) { int id = 0; while (*str != '\0') { if (nodes[id].child[*str - 'a'] == -1) nodes[id].child[*str - 'a'] = newNode(); id = nodes[id].child[*str - 'a']; str++; } return ++nodes[id].cnt; } int remove(char str[]) { return calc(str, 0, 0); } int search(char str[]) { return calc(str, 0, 1); }
Editor is loading...
Leave a Comment