#include#include#include#include#include#include#include#include#include#include

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

統計難題

系統 1630 0

http://acm.hdu.edu.cn/showproblem.php?pid=1251

通過這道題學習一下Trie字典樹.

      #include <iostream>
      
        

#include 
      
      <fstream>
      
        

#include 
      
      <vector>
      
        

#include 
      
      <
      
        string
      
      >
      
        

#include 
      
      <algorithm>
      
        

#include 
      
      <map>
      
        

#include 
      
      <stack>
      
        

#include 
      
      <cmath>
      
        

#include 
      
      <queue>
      
        

#include 
      
      <
      
        set
      
      >
      
        

#include 
      
      <list>
      
        

#include 
      
      <cctype>
      
        

#include 
      
      <stdio.h>
      
        

#include 
      
      <stdlib.h>
      
        

#include 
      
      <
      
        string
      
      .h>


      
        #define
      
       REP(i,j,k) for(int i = j ; i < k ; ++i)


      
        #define
      
       MAXV (1000)


      
        #define
      
       INF (0x6FFFFFFF)


      
        #define
      
       MAX 26


      
        using
      
      
        namespace
      
      
         std;

typedef 
      
      
        struct
      
      
         Trie

{

    Trie 
      
      *
      
        next[MAX];

    
      
      
        int
      
       v;
      
        //
      
      
        這里表示該字符前綴出現的次數
      
      
        };

Trie 
      
      *
      
        root;




      
      
        void
      
       Insert(
      
        char
      
       *
      
        str)

{

    Trie 
      
      *current =
      
         root;

    
      
      
        int
      
        len=
      
        strlen(str);

    
      
      
        if
      
      (len==
      
        0
      
      ) 
      
        return
      
      
         ;

    REP(i,
      
      
        0
      
      
        ,len)

    {

        
      
      
        //
      
      
        若節點存在,則直接訪問下一個節點,并此前綴出現次數+1
      
      
        if
      
      (current->next[str[i]-
      
        '
      
      
        a
      
      
        '
      
      ]!=
      
        NULL)

        {

            current
      
      =current->next[str[i]-
      
        '
      
      
        a
      
      
        '
      
      
        ];

            current
      
      ->v++
      
        ;

        }

        
      
      
        //
      
      
        若節點不存在,則新建節點并初始化
      
      
        else
      
      
        

        {

            current
      
      ->next[str[i]-
      
        '
      
      
        a
      
      
        '
      
      ]=(Trie*)malloc(
      
        sizeof
      
      
        (Trie));

            current
      
      =current->next[str[i]-
      
        '
      
      
        a
      
      
        '
      
      
        ];

            REP(i,
      
      
        0
      
      ,
      
        26
      
      ) current->next[i]=
      
        NULL;

            current
      
      ->v=
      
        1
      
      
        ;

        }

    }

}


      
      
        int
      
       Find(
      
        char
      
       *
      
        str)

{

    
      
      
        int
      
       len=
      
        strlen(str);

    
      
      
        struct
      
       Trie *current=
      
        root;

    
      
      
        if
      
      (len==
      
        0
      
      ) 
      
        return
      
      
        0
      
      
        ;

    REP(i,
      
      
        0
      
      
        ,len)

    {

        
      
      
        //
      
      
        如果關鍵詞本字符存在,則訪問下一個節點
      
      
        if
      
      (current->next[str[i]-
      
        '
      
      
        a
      
      
        '
      
      ]!=
      
        NULL)

            current
      
      =current->next[str[i]-
      
        '
      
      
        a
      
      
        '
      
      
        ];

        
      
      
        else
      
      
        return
      
      
        0
      
      ;
      
        //
      
      
        不存在直接返回0
      
      
            }

    
      
      
        return
      
       current->v;
      
        //
      
      
        返回該前綴出現次數
      
      
        }


      
      
        void
      
      
         Init()

{

    root
      
      =(Trie *)malloc(
      
        sizeof
      
      
        (Trie));

    REP(i,
      
      
        0
      
      ,
      
        26
      
      ) root->next[i]=
      
        NULL;

    root
      
      ->v=
      
        1
      
      
        ;

}


      
      
        int
      
      
         main()

{

    Init();

    
      
      
        char
      
       str[
      
        11
      
      
        ];

   
      
      
        //
      
      
         freopen("in.txt","r",stdin);
      
      
        while
      
      (gets(str)&&strcmp(str,
      
        ""
      
      )!=
      
        0
      
      
        )

            Insert(str);

    
      
      
        char
      
       tmp[
      
        11
      
      
        ];

    
      
      
        while
      
      (~scanf(
      
        "
      
      
        %s
      
      
        "
      
      
        ,tmp))

        printf(
      
      
        "
      
      
        %d\n
      
      
        "
      
      
        ,Find(tmp));

    
      
      
        return
      
      
        0
      
      
        ;

}
      
    

?

統計難題


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久中文字幕网 | 男人你懂的网站 | 免费毛片视频 | 久久中文娱乐网 | 欧美18videosex灌满 | 成人国产在线视频 | 久青草国产手机在线观 | 国产99热在线观看 | 日本一级特黄毛片免费视频9 | 日韩国产成人资源精品视频 | 性做久久久久久久久男女 | 婷婷婷色 | 欧美亚洲网 | 人人干操 | 色综合久久天天综线观看 | 国产亚洲综合成人91精品 | 久热亚洲| 亚洲成人第一 | 99精品视频在线观看免费播放 | 国产一区二区三区播放 | 中文字幕日韩女同互慰视频 | 国产视频一区在线播放 | 奇米影视在线播放 | 91色综合久久 | 97久久精品国产成人影院 | 欧美国产片 | 精品国产a | 色网站综合 | 久久夜色撩人精品国产 | 青草视频网 | 日本a毛片在线播放 | 国产精品v | 青青青国产免费手机视频在线观看 | 尤物国产在线精品福利一区 | 高清不卡日本v在线二区 | 色久综合大榴莲 | 久久精品视频热 | 狠狠色成人综合首页 | 国产在线综合视频 | 99热在线只有精品 | 日本囗交做爰视频欧美 |