image.png

include

include

include

include

include

include

include

include

include

include

using namespace std;

define BASE 26

define BASE_LETTER ‘a’

typedef struct Node { int flag;//标记是否独立成词 —— 数据域 int qu; struct Node *next[BASE];//指向26条边 ——指针域 } Node;

Node getNewNode() {//申请新节点 Node p = (Node *)calloc(sizeof(Node), 1); return p; }

inline int code(char ch) { return ch - BASE_LETTER; }

void insert(Node root, char str) {//字典树的插入操作 Node *p = root; for (int i = 0; str[i]; i++) { if (p->next[code(str[i])] == NULL) p->next[code(str[i])] = getNewNode(); p = p->next[code(str[i])]; } p->flag = 1; p->qu = 0; return; }

int query(Node root, char str) { Node *p = root; for (int i = 0; str[i]; i++) { p = p->next[code(str[i])]; if (p == NULL) return 0; } if (p->flag == 0) { return 0; } if (p->flag == 1 && p->qu == 0) { p->qu = 1; return 1;//是单词,没重复 } if (p->flag == 1 && p->qu == 1) { return 2;//重复了 }

}

void output(Node root, int k, char buff) { if (root == NULL) return; if (root->flag) printf(“%s\n”, buff); buff[k + 1] = ‘\0’; for (int i = 0; i < BASE; i++) { buff[k] = BASE_LETTER + i; output(root->next[i], k + 1, buff); } return ; }

void clear(Node *root) {//字典树的销毁操作 if (root == NULL) return; for (int i = 0; i < BASE; i++) { clear(root->next[i]); } free(root); }

int main() { char str[1000]; int n; Node *root = getNewNode(); scanf(“%d”, &n); for (int i = 0 ; i < n; i++) { scanf(“%s”, str); insert(root, str); } int m; scanf(“%d”, &m); while (m—) { scanf(“%s”, str); int ans = query(root, str); if (ans == 0) { printf(“WRONG\n”); } if (ans == 1) { printf(“OK\n”); } if (ans == 2) { printf(“REPEAT\n”); } }

  1. return 0;

}