• <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>

            ACM___________________________

            ______________白白の屋
            posts - 182, comments - 102, trackbacks - 0, articles - 0
            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            常用鏈接

            留言簿(24)

            隨筆分類(332)

            隨筆檔案(182)

            FRIENDS

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋    

             

            題目地址 :

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

            題目描述:

            Hat’s Words

            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
            Total Submission(s): 1384    Accepted Submission(s): 506


            Problem Description
            A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
            You are to find all the hat’s words in a dictionary.
             

            Input
            Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
            Only one case.
             

            Output
            Your output should contain all the hat’s words, one per line, in alphabetical order.
             

            Sample Input
            a ahat hat hatword hziee word
             

            Sample Output
            ahat hatword
             

            題目分析 :

              

                    字典樹(shù)的題目, 這個(gè)題的方法比較暴力,  在輸入的時(shí)候?qū)⒚總€(gè)單詞都加入字典樹(shù),  并用數(shù)組將所有的串都存起來(lái)來(lái).

            在輸入完成后,  對(duì)每個(gè)單詞進(jìn)行拆分, 也就是暴力枚舉單詞不同位置的前后部分, 看在字典樹(shù)中是否存在, 存在即輸出.

            不過(guò)貌似數(shù)據(jù)比較弱, 說(shuō)是按字典順序輸出, 但其實(shí)他的輸入本就是按字典順序輸入的, 所以將排序也面了 , 呵呵.

                另外,其實(shí)這題用STL 做更簡(jiǎn)單, 當(dāng)然追求效率除外.

            trie 代碼:

             /*

            MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋

                      http://www.cnblog.com/MiYu

            Author By : MiYu

            Test      : 1

            Program   : 1247

            */


            #include <iostream>

            #include <algorithm>

            #include <string>

            using namespace std;

            typedef struct dictor DIC;

            DIC *root = NULL;

            struct dictor {

                   dictor (){ exist = false; memset ( child , 0 , sizeof ( child ) ); }

                   void insert ( char *ins );

                   bool find ( const char *ins );

            private:

                   DIC *child[26];

                   bool exist; 

            };

            void dictor::insert ( char *ins )

            {

                        DIC *cur = root,*now;

                        int len = strlen ( ins );

                        for ( int i = 0; i < len; ++ i )

                        {

                              if ( cur->child[ ins[i] - 'a' ] != NULL ) 

                              {

                                   cur = cur->child[ ins[i] - 'a' ];

                              }

                              else

                              {

                                   now = new DIC;

                                   cur->child[ ins[i] - 'a' ] = now;

                                   cur = now;

                              }

                        } 

                        cur->exist = true;

            }

            bool dictor::find ( const char *ins )

            {

                        DIC *cur = root;

                        int len = strlen ( ins );

                        for ( int i = 0; i < len; ++ i )

                        {

                             if ( cur->child[ ins[i] - 'a' ] != NULL )

                                  cur = cur->child[ ins[i] - 'a' ];  

                             else

                                  return false; 

                        } 

                        return cur->exist;

            }

            char words[50050][100];

            char s1[100],s2[100];

            DIC dict;

            int main ()

            {

                root = &dict;

                int n = 0;

                while ( scanf ( "%s",words[n] ) != EOF )

                {

                        dict.insert ( words[n++] );

                }

                for ( int i = 0; i < n; ++ i )

                {

                      int len = strlen ( words[i] );

                      for ( int j = 1; j < len; ++ j )

                      {

                            memset ( s1, 0, sizeof ( s1 ) );

                            memset ( s2, 0, sizeof ( s2 ) );

                            strncpy ( s1, words[i], j );

                            strcpy ( s2, words[i]+j );

                            if ( dict.find ( s1 ) && dict.find ( s2 ) )

                            {

                                 printf ( "%s\n", words[i] );

                                 break; 

                            }

                      }  

                }  

                //system ( "pause" );

                return 0;

            }


            STL  代碼 :

             

             /*

            MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋

                      http://www.cnblog.com/MiYu

            Author By : MiYu

            Test      : 2

            Program   : 1247

            */


            #include <iostream>

            #include <string>

            #include <map>

            using namespace std;

            map < string , int > mp;

            string str[50005];

            int main ()

            {

                int n = 0;

                while ( cin >> str[n] ) mp[ str[n++] ] = 1;

                for ( int i = 0; i < n; ++ i )

                {

                      unsigned len = str[i].size ();

                      for ( unsigned j = 1; j < len; ++ j )

                      {

                           string s1 ( str[i], 0, j );

                           string s2 ( str[i], j );

                           if ( mp[s1] == 1 && mp[s2] == 1 )

                           {

                                cout << str[i] << endl; 

                                break;

                           }

                      } 

                }

                return 0;

            }


             

             

            久久综合综合久久综合| 国产成人精品久久亚洲| 亚洲日韩中文无码久久| 99久久精品免费看国产一区二区三区| 国产成年无码久久久免费| 久久午夜伦鲁片免费无码| 久久99国产精品久久久| 亚洲国产精品嫩草影院久久| 中文字幕人妻色偷偷久久| 国产精品18久久久久久vr| 久久久久99精品成人片牛牛影视| 青青草原综合久久大伊人导航| 精品一二三区久久aaa片| 免费国产99久久久香蕉| 久久精品国产99国产精品导航| 国产高清美女一级a毛片久久w| 亚洲愉拍99热成人精品热久久 | 亚洲精品午夜国产va久久| 国产日产久久高清欧美一区| 亚洲第一永久AV网站久久精品男人的天堂AV | 亚洲欧洲精品成人久久奇米网| 久久久精品人妻一区二区三区蜜桃| 久久激情亚洲精品无码?V| 99国产欧美精品久久久蜜芽| 国内高清久久久久久| 久久久久久综合网天天| 久久久噜噜噜久久熟女AA片| 欧美大战日韩91综合一区婷婷久久青草| 久久久久久久97| 青春久久| 亚洲欧美国产精品专区久久| 亚洲国产精品婷婷久久| 久久99精品综合国产首页| 久久99亚洲网美利坚合众国| 中文字幕无码免费久久| 欧美亚洲色综久久精品国产| 午夜欧美精品久久久久久久| 中文字幕久久精品无码| 久久亚洲AV无码精品色午夜| 亚洲精品白浆高清久久久久久| 国产精品美女久久久久久2018|