E - Compound Words 解题心得

系统 2077 0

原题贴上

 

 

10391 Compound Words

 


You are to find all the two-word compound words in a dictionary. A two-word compound word is a
word in the dictionary that is the concatenation of exactly two other words in the dictionary.
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will
be no more than 120,000 words.
Output
Your output should contain all the compound words, one per line, in alphabetical order.
Sample Input
a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra
Sample Output
alien
newborn

 

 

 

分析:最好的方法是用hash算法,可是我暂时还不会这种算法。还好可以用map存储字典再查找,

然而这里有2中查找方式   一、以输入单词作为查找对象 ,这就需要将所有输入的单词2—2组合存为一个字典,再遍历输入的单词看是否能找到,这里的输入数据会很大,可能达到120000,这种查找方式的复杂度o(n^2),如果这样做会TLE

方式二 、已输入单词的部分作为对象,这样就不用将单词2-2组合,而只需要将一个单词拆分,一个单词最多也就40个字母吧,那么遍历左右拆分后的单词的复杂度只有 o(n*40)    也就是o(n),这样就不会TLE了

 

 

下面贴上方式二 的代码

        #include <iostream>
        
          

#include 
        
        <cstdlib>
        
            

#include 
        
        <cstdio>
        
            

#include 
        
        <cstring>
        
            

#include 
        
        <map>  


        
          using
        
        
          namespace
        
        
           std;



map
        
        <
        
          string
        
        , 
        
          int
        
        >
        
           Map;




        
        
          int
        
        
           n;




        
        
          char
        
         str[
        
          120010
        
        ][
        
          30
        
        
          ];




        
        
          int
        
        
           main()

{

    Map.clear();

    n 
        
        = 
        
          0
        
        
          ;

    
        
        
          while
        
         (~scanf(
        
          "
        
        
          %s
        
        
          "
        
        
          , str[n])){

        Map[str[n]] 
        
        = 
        
          1
        
        
          ;

        n
        
        ++
        
          ;

    }





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

    {

        
        
        
          int
        
         len =
        
           strlen(str[i]);

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

        {

            
        
        
          char
        
         temp1[
        
          30
        
        ] = { 
        
          '
        
        
          \0
        
        
          '
        
        
           };

            
        
        
          char
        
         temp2[
        
          30
        
        ] = { 
        
          '
        
        
          \0
        
        
          '
        
        
           };

            strncpy(temp1, str[i], j);

            strncpy(temp2, str[i] 
        
        + j, len -
        
           j);

            
        
        
          if
        
         (Map[temp1] &&
        
           Map[temp2])

            {

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

                
        
        
          break
        
        
          ;

            }

        }

    }



    
        
        
          return
        
        
          0
        
        
          ;

}
        
      
View Code

 

E - Compound Words 解题心得


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论