第九届中山大学校赛预选赛(2006)
Problem A 信息泛滥
大致题意:给出 n 个不同的字符串,再给出 m 个字符串,问这 m 个字符串中在给出的 n 个串中没有出现的个数。
思路:建 Trie ,统计即可。
----------------------------------------------------------
# include <stdio.h> struct tree { tree *node[26]; int bj; tree() { bj = 0; for (int i = 0; i < 26; ++i) node[i] = NULL; } } *root; int n, m, ans; char s[11]; void update(char *str) { for (int i = 0; str[i]; ++i) if ('A'<=str[i] && str[i]<='Z') str[i] += 'a'-'A'; } void visit(tree *p, char *ch, int bj) { char c = (*ch) - 'a'; if (p->node[c]) p = p->node[c]; else { if (!bj) return ; p->node[c] = new tree; p = p->node[c]; } if (ch[1]) visit(p, ch+1, bj); else { if (p->bj != bj) ans += bj ? 1:-1; p->bj = bj; } } void release(tree *p) { for (int i = 0; i < 26; ++i) if (p->node[i]) release(p->node[i]); delete p; } int main() { while (scanf("%d%d", &n, &m), n) { root = new tree; for (int i = 0; i < n; ++i) { scanf("%s", s); update(s); visit(root, s, 1); } for (int j = 0; j < m; ++j) { scanf("%s", s); update(s); visit(root, s, 0); } printf("%d\n", ans); release(root); } return 0; }
----------------------------------------------------------