zoukankan      html  css  js  c++  java
  • HDU 1251

    用字典树统计个数。

    一定要交C++。G++MLE。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <string>
     5 #include <cstdlib>
     6 using namespace std;
     7 struct Trie
     8 {
     9     Trie *next[26];
    10     int count;
    11     Trie()
    12     {
    13         count=0;
    14         for(int i=0;i<26;i++) next[i]=NULL;
    15     }
    16 };
    17 void insert(Trie *s,char x[])
    18 {
    19     int k;
    20     for(int i=0;x[i];i++)
    21     {
    22         k=x[i]-'a';
    23         if(s->next[k]==NULL)
    24             s->next[k]=new Trie;
    25         s=s->next[k];
    26         ++s->count;
    27     }
    28 }
    29 int find(Trie *s,char x[])
    30 {
    31     int k;
    32     for(int i=0;x[i];i++)
    33     {
    34         k=x[i]-'a';
    35         if(s->next[k]) s=s->next[k];
    36         else return 0;
    37     }
    38     return s->count;
    39 }
    40 int main()
    41 {
    42     int num=0;
    43     Trie* root=new Trie;
    44     char s[20];
    45     while(gets(s),strcmp(s,""))
    46     {
    47         insert(root,s);
    48     }
    49     while(gets(s)!=NULL)
    50     {
    51         printf("%d
    ",find(root,s)) ;
    52     }
    53 }
    我自倾杯,君且随意
  • 相关阅读:
    gan研究思路
    官方文档的学习
    构建Pytorch虚拟环境
    桌面显示【我的电脑】
    【VUE】计数器模块
    【品优购】字体图标定位的做法
    029垃圾分代回收机制
    03特殊for语句
    28包 package
    jdk特性
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5682763.html
Copyright © 2011-2022 走看看