zoukankan      html  css  js  c++  java
  • 字典树基础

    int  trie[400100][26];
    int sum[400100];
    int pos=1;
    void insert1(char *s)
    {
        int root=0;
        for(int i=0;i<strlen(s);i++)
        {   int ch=s[i]-'a';
            if( trie[ root ][ch]==0  )
              {
                 memset(trie[pos],0,sizeof(trie[pos]));//用多少初始化多少
                 trie[root][ch]=pos++;
              }
            root=trie[root][ch];
        }
        sum[root]=1;
    }
    
    int find1(char *s)
    {
        int root=0;
        for(int i=0;i<strlen(s);i++)
        {
            int ch=s[i]-'a';
            if( trie[root][ch]==0 )return false;
            root=trie[root][ch];
        }
        return sum[root];
    
    }
    int main()
    {
        pos=1;
        memset(sum,0,sizeof(sum));
        memset(trie[0],0,sizeof(trie[0]));
        
        
        
        return 0;
    }

    指针写法

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    char s[11];
    int n,m;
    bool p;
    struct node
    {
        int count;
        node * next[26];
    }*root;
    node * build()
    {
        node * k=new(node);
        k->count=0;
        memset(k->next,0,sizeof(k->next));
        return k;
    }
    void insert()
    {
        node * r=root;
        char * word=s;
         while(*word)
        {
            int id=*word-'a';
            if(r->next[id]==NULL) r->next[id]=build();
            r=r->next[id];
            r->count++;
            word++;
        }
    }
    int search()
    {
        node * r=root;
        char * word=s;
        while(*word)
        {
            int id=*word-'a';
            r=r->next[id];
            if(r==NULL) return 0;
            word++;
        }
        return r->count;
    }
    int main()
    {
        root=build();
        scanf("%d",&n);
        for(int i=1;i<=n;i++) 
        {
                cin>>s;
                insert();
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            cin>>s;
            printf("%d
    ",search());
        }
    }
    View Code
  • 相关阅读:
    c++智能指针的一些文章
    c++ template(8)模版多态
    空指针赋值分区
    windbg调试命令
    c++ template(5)模板实战
    GetStockObject 理解
    c++ template(6)模板术语
    c++ template(71)模板参数声明
    DirectDraw教程资料
    c++ template(9)trait和Policy
  • 原文地址:https://www.cnblogs.com/bxd123/p/10342068.html
Copyright © 2011-2022 走看看