• 【hiho一下第二周 】Trie树


    【题目链接】:http://hihocoder.com/problemset/problem/1014

    【题意】

    【题解】

    在字典树的域里面加一个信息cnt;
    表示这个节点下面,记录有多少个单词;
    在找的时候,直接到那个节点;
    然后输出这个cnt就好了

    【Number Of WA

    0

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 100e4+100;
    
    struct node
    {
        int child[27],cnt;
    };
    
    node tree[N];
    int n,root = 1,tot = 1;
    char s[15];
    
    int main()
    {
        //freopen("F:\\rush.txt","r",stdin);
        ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
        cin >> n;
        rep1(i,1,n)
        {
            cin >> s;
            int len = strlen(s);
            int now = root;
            rep1(j,0,len-1)
            {
                int t = s[j]-'a'+1;
                if (tree[now].child[t]==0)
                    tree[now].child[t]=++tot;
                now = tree[now].child[t];
                tree[now].cnt++;
            }
        }
        cin >> n;
        rep1(i,1,n)
        {
            cin >> s;
            int len = strlen(s);
            int now = root;
            rep1(j,0,len-1)
            {
                int t = s[j]-'a'+1;
                now = tree[now].child[t];
            }
            cout << tree[now].cnt << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    BUG记录
    .Net HTTP请求的发送方式与分析
    初始token
    VS2017开发安卓应用(Xamarin)
    路由模板和路由特性
    使用signalR创建聊天室。
    C# SessionHelper
    postgres递归查询所有子部门
    centos7备份postgres
    Centos7挂载硬盘
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626356.html
Copyright © 2020-2023  润新知