zoukankan      html  css  js  c++  java
  • hdu 1251简单字典树

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    using namespace std;
    struct node {
    int num,i;
    node *a[27];
    node (){
    num=0;
    for(i=0;i<27;i++)
    a[i]=NULL;
    }
    }*root;
    char str[20];
    void insert(int len,node *root) {
    int i;
    node *q;
    for(i=0;i<len;i++) {
    if(root->a[str[i]-'a']==NULL) {
    q=new node();
    q->num++;
    root->a[str[i]-'a']=q;
    root=q;
    }
    else  {
    root=root->a[str[i]-'a'];
    root->num++;
    }
    }
    return ;
    }
    int find(int len,node *root) {
    int i;
    for(i=0;i<len;i++) {
    root=root->a[str[i]-'a'];
    if(root==NULL)
    return 0;
    }
    return root->num;
    }
    int main() {
    int k;
    root=new node();
    while(gets(str)&&str[0]!='') //输入条件太恶心了
    insert(strlen(str),root);
    while(scanf("%s",str)!=EOF) {
    k=find(strlen(str),root);
    printf("%d ",k);
    }
    return 0;
    }



  • 相关阅读:
    Docker
    Docker
    VIM
    Python
    Python
    VIM
    Python
    其他
    Java
    Java
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410918.html
Copyright © 2011-2022 走看看