• Trie字典树


    第九届中山大学校赛预选赛(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;
    }
     

    ----------------------------------------------------------

  • 相关阅读:
    HDU 2023题解分析
    Java中常见的几种类型转换
    Software Version --hdu1976
    单词数
    Usaco 2.3 Zero Sums(回溯DFS)--暴搜
    9的余数
    mongodb学习(一)
    svg学习(九)path
    svg学习(八)polyline
    qunit学习(一)
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2585592.html
Copyright © 2020-2023  润新知