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

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

統(tǒng)計(jì)難題

系統(tǒng) 1894 0

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

通過這道題學(xué)習(xí)一下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;
      
        //
      
      
        這里表示該字符前綴出現(xiàn)的次數(shù)
      
      
        };

Trie 
      
      *
      
        root;




      
      
        void
      
       Insert(
      
        char
      
       *
      
        str)

{

    Trie 
      
      *current =
      
         root;

    
      
      
        int
      
        len=
      
        strlen(str);

    
      
      
        if
      
      (len==
      
        0
      
      ) 
      
        return
      
      
         ;

    REP(i,
      
      
        0
      
      
        ,len)

    {

        
      
      
        //
      
      
        若節(jié)點(diǎn)存在,則直接訪問下一個(gè)節(jié)點(diǎn),并此前綴出現(xiàn)次數(shù)+1
      
      
        if
      
      (current->next[str[i]-
      
        '
      
      
        a
      
      
        '
      
      ]!=
      
        NULL)

        {

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

            current
      
      ->v++
      
        ;

        }

        
      
      
        //
      
      
        若節(jié)點(diǎn)不存在,則新建節(jié)點(diǎn)并初始化
      
      
        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)

    {

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

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

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

    
      
      
        return
      
       current->v;
      
        //
      
      
        返回該前綴出現(xiàn)次數(shù)
      
      
        }


      
      
        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
      
      
        ;

}
      
    

?

統(tǒng)計(jì)難題


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 太仓市| 姚安县| 内黄县| 绿春县| 老河口市| 陇西县| 天全县| 临夏县| 贵南县| 巢湖市| 察哈| 庆云县| 历史| 长沙市| 桃园市| 年辖:市辖区| 博罗县| 宁波市| 南岸区| 若羌县| 池州市| 上林县| 佛教| 永泰县| 滕州市| 永安市| 日照市| 凌云县| 资中县| 镇江市| 秭归县| 金湖县| 石门县| 衢州市| 天水市| 西吉县| 齐河县| 印江| 招远市| 蒙城县| 丰都县|