#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條評論
主站蜘蛛池模板: 99国产精品免费视频观看 | 高清一区二区三区 | 日韩精品欧美亚洲高清有无 | 天天干天天草天天射 | 日本高清中文字幕在线观穿线视频 | 模特啪啪 | 亚洲成a人v大片在线观看 | 美国成人毛片 | 黄色.www| 日韩亚洲一区中文字幕在线 | 91精品国产爱久久久久久 | 久久精品视频8 | 成人久草| 欧美a视频 | 99久久精品久久久久久清纯 | 欧美乱爱 | 欧美啪| 亚洲一区二区三区四区热压胶 | 国产青青久久 | 一级毛片观看 | 中文字幕 亚洲精品 | 四虎4hu永久免费视频大全 | vr欧美乱强伦xxxxx | 国产大尺度福利视频在线 | 欧美一区二区三区在线 | 九九九九热精品视频 | 五月激情婷婷综合 | 亚洲高清专区 | 精品欧美一区二区精品久久 | 欧美成人免费一区在线播放 | 91久久亚洲精品国产一区二区 | 性xxxxxx| 成人在线视频网址 | 国产欧美日韩精品综合 | 日韩中文字幕免费观看 | 99视频免费观看 | 国产swag在线观看 | 午夜婷婷| 国产成人午夜性a一级毛片 国产成人午夜性视频影院 国产成人系列 | 欧美激情视频一区 | 国产美女色视频 |