• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            gzwzm06

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              1 隨筆 :: 52 文章 :: 17 評論 :: 0 Trackbacks
            #include <stdio.h>
            #include 
            <string>
            #include 
            <algorithm>
            using namespace std ;

            struct WORD    
            {
                
            int ID ;    //模式串編號,處理重復
                WORD *next ;
            }
             ;

            const int MAXN = 100001 ;
            const int MAXM = 101 ;
            int N, M ;
            int match[MAXN] , num ;    //保存匹配結果
            char words[MAXM][25] ;

            WORD g_Temp[MAXN];
            int g_pos ;

            const int CAP = 28 ;
            typedef 
            struct NODE    //Trie 節點
            {    
                NODE()
                
            {
                    m_Pos.next 
            = NULL ;
                    current 
            = &m_Pos ;
                    memset(next, NULL, 
            sizeof(NODE));
                }
            ;
                NODE 
            *next[CAP];
                WORD m_Pos ;        
            //處理重復
                WORD *current ;
            }
            NODE;

            const int MEMORY = 30000 ;
            NODE memory[MEMORY] ;
            class BTree
            {
            public:
                BTree()
                
            {
                    index 
            = 1;
                    head 
            = &memory[0];
                }
                
                
            void Insert(char *str, const int& w)
                
            {
                    
            int len = (int)strlen(str);
                    NODE 
            *pt = head ;
                    
            for (int i = 0; i < len; ++i)
                    
            {
                        
            if ( str[i] == '*' )
                        
            {
                            
            if ( pt->next[26== NULL )
                            
            {
                                pt
            ->next[26= &memory[index++] ;
                            }

                            pt 
            = pt->next[26] ;        
                            
                        }

                        
            else if ( str[i] == '?' )
                        
            {
                            
            if ( pt->next[27== NULL )
                            
            {
                                pt
            ->next[27= &memory[index++] ;
                            }

                            
                            pt 
            = pt->next[27] ;
                        }

                        
            else {
                            
            if (pt->next[str[i]-'a'== NULL)
                            
            {
                                pt
            ->next[str[i]-'a'= &memory[index++];
                            }

                            
                            pt 
            = pt->next[str[i]-'a'];
                        }

                    }

                    
            //記錄模式串編號
                    if ( pt->m_Pos.next == NULL )
                        pt
            ->current = &pt->m_Pos ;
                    g_Temp[g_pos].ID 
            = w ;
                    g_Temp[g_pos].next 
            = NULL ;
                    pt
            ->current->next = &g_Temp[g_pos++] ;
                    pt
            ->current = pt->current->next ;
                    
                }

                
                
            void DFS( NODE *ptr, const int& pos , int w )
                
            {
                    
            if ( words[pos][w] == '\0' )
                    
            {
                        WORD 
            *= ptr->m_Pos.next ;
                        
            while ( p ){
                            match[num] 
            = p->ID ;
                            num
            ++ ;
                            p 
            = p->next ;
                        }


                        
            while ( ptr->next[26] )
                            ptr 
            = ptr->next[26] ;
                        p 
            = ptr->m_Pos.next ;
                        
            while ( p ){
                            match[num] 
            = p->ID ;
                            num
            ++ ;
                            p 
            = p->next ;
                        }

                        
            return ;
                    }

                    
            //遇到'*',將當前位置到結束符的字符一個一個匹配
                    if ( ptr->next[26!= NULL )
                    
            {
                        
            for ( int i = w ; i <= strlen(words[pos]) ; ++i )
                        
            {
                            DFS( ptr
            ->next[26], pos, i ) ;
                        }

                    }

                    
            //遇到'?'或字母,直接跳到下一個
                    if ( ptr->next[27!= NULL )
                    
            {
                        DFS( ptr
            ->next[27], pos, w + 1 ) ;
                    }

                    
            if ( ptr->next[words[pos][w] - 'a'!= NULL )
                    
            {
                        DFS( ptr
            ->next[words[pos][w] - 'a'], pos, w + 1 ) ;
                    }

                }

                
            public:
                NODE 
            *head;
                
            int index;
            }
            ;

            void Init()
            {
                g_pos 
            = 0 ;
            }


            int main()
            {    
                
            int i ;
                
            char tempstr[22] ;
                BTree trie ;
                scanf(
            "%d %d"&N, &M) ;
                Init() ;
                
                
            for ( i = 0 ; i < N ; ++i )
                
            {
                    scanf(
            "%s"&tempstr) ;
                    trie.Insert(tempstr, i) ;
                }

                
            for ( i = 0 ; i < M ; ++i )
                
            {
                    scanf(
            "%s"&words[i]) ;    
                    num 
            = 0 ;
                    trie.DFS( trie.head, i, 
            0 ) ;
                    
                    
            if ( num == 0 )
                    
            {
                        printf(
            "Not match\n") ;
                    }

                    
            else {
                        sort( match, match 
            + num ) ;
                        printf(
            "%d ", match[0]) ;
                        
            for ( int j = 1 ; j < num ; ++j )
                        
            {
                            
            if ( match[j] != match[j - 1] )
                                printf(
            "%d ", match[j]) ;
                        }

                        printf(
            "\n") ;
                    }

                }

                
                
            return 0 ;
            }
            posted on 2008-11-09 17:05 閱讀(414) 評論(0)  編輯 收藏 引用 所屬分類: 字符串處理
            精品久久无码中文字幕| 久久91这里精品国产2020| 精品久久久久久无码不卡| 人妻精品久久久久中文字幕69 | 狠狠久久综合| 久久99热这里只有精品66| 国产三级久久久精品麻豆三级| 久久综合丁香激情久久| 久久久久久午夜精品| 青青草国产成人久久91网| 国产成人精品综合久久久久| 91麻精品国产91久久久久| av色综合久久天堂av色综合在| 亚洲一区二区三区日本久久九| 成人午夜精品无码区久久| 久久婷婷五月综合97色直播| 国产精品美女久久久久久2018| 久久只有这精品99| 久久精品国产WWW456C0M| 99国产精品久久| 无码人妻精品一区二区三区久久久 | 中文字幕人妻色偷偷久久| 国产一区二区三精品久久久无广告 | 久久婷婷激情综合色综合俺也去| 香港aa三级久久三级| 奇米影视7777久久精品| 亚洲欧洲精品成人久久奇米网| 情人伊人久久综合亚洲| 国内精品伊人久久久久| AV无码久久久久不卡网站下载| 人妻无码精品久久亚瑟影视| 久久人人爽人人爽人人片AV麻豆 | AV色综合久久天堂AV色综合在| 久久无码AV一区二区三区| 亚洲国产精品久久66| 中文字幕久久欲求不满| 久久这里只精品国产99热| 很黄很污的网站久久mimi色| 久久国产福利免费| 亚洲精品无码久久久| 久久久久久久波多野结衣高潮 |