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

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

統計難題

系統 1692 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條評論
主站蜘蛛池模板: 99热这里只有免费国产精品 | 丝袜捆绑调教视频免费区 | 国产视频日韩 | 欧美成人18性 | 日本精品久久久久久久久免费 | 久久夜夜操妹子 | 色影影院 | 欧美人两个人激情的免费视频 | 四虎综合 | 看中国毛片 | 亚洲日韩欧美视频 | 国产精品尤物在线 | 天天爱夜夜爽 | 久草精彩视频 | 五月激情综合婷婷 | 毛片短视频 | 久久国产亚洲观看 | 亚洲欧美日韩精品久久 | k8久久久一区二区三区 | 亚洲精品久久久一区二区三区 | 91伊人 | 夜夜爽日日澡人人 | www.99精品视频在线播放 | 视频在线观看一区 | 五月天丁香久久 | 国产精品久久久爽爽爽麻豆色哟哟 | 人人九九| 亚洲一区二区av | 看一天影院宅急看在线观看 | 摸金校尉之九幽将军 | 亚洲欧美第一视频 | 日本无码少妇波多野结衣 | 欧美日韩一区,二区,三区,久久精品 | 精品久久久久久久人人人人传媒 | 欧美激情综合色综合啪啪五月 | 欧洲在线观看 | 情侣av| 日产乱码卡一卡2卡三卡四麻豆 | 精品国产一区二区三区四 | 波多野结衣 久久 | 国产精品无码专区在线观看 |