zoukankan      html  css  js  c++  java
  • 找复合单词

    题意:

          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

    思路:

           一个字:拆!(不知为何如此想笑,笑cry)。用集合set把全部的单词存起来,然后把每个单词都拆开。

    譬如:newborn。第一次拆成n和ewborn,然后进行判断在set里面能否找到这两个单词,不能就继续拆,

    第二次ne和wborn。。。。直到new跟born。详细代码如下:

    源代码:

     1 #include<iostream>
     2 #include<string>
     3 #include<set>
     4 #include<sstream>
     5 using namespace std;
     6 set<string>dict;
     7 set<string>dicc;         //定义两个集合
     8 int main()
     9 {
    10     string str;
    11     do
    12     {
    13         getline(cin, str);
    14         dict.insert(str);
    15     } while (str.length() > 0);         //输入单词,以空行结束,把每个单词都存进集合中
    16 
    17 
    18     for (set<string>::iterator it = dict.begin(); it != dict.end(); it++)  //迭代器遍历每个单词
    19     {
    20         string a = *it;                                                     //把其中一个单词取出来,拆!
    21         for (int i = 1; i < a.length(); i++)
    22         {                                     
    23             string frist = a.substr(0, i);                                   //把一个单词拆成两部分
    24             string next = a.substr(i, a.length());
    25 
    26             if (dict.find(frist) != dict.end() && dict.find(next) != dict.end())  //判断两个子单词是否在集合里
    27             {
    28                 cout << a << endl;                                //找到了!就输出~~~~~~~
    29                 break;
    30             }
    31         }
    32 
    33     }
    34 
    35     //system("pause");
    36     return 0;
    37 }

    心得:

    本题有两个思路,一个是拆,那么另一个就是拼,拆的思路上面讲了,至于拼,就是每两个单词都拼一次,然后用迭代器在set找找。能找到,你就是棒棒哒(=_=然而我现在还没写出来,默默看着。。。。),集合以前没用过,这道题用了以后才觉得好神奇,整个单词可以看成一个元素insert到set去,需要用时再用迭代器取出来。迭代器从头遍历到尾的功能也可以省下很多代码,但是感觉还不是很熟练。(亲~别忘了头文件)

       本题还用了一个取字符串的函数substr函数,(从第几个数开始取,到上面时候结束)

    格式:substr(string,start,length)

    每天学点知识还是挺开心的~~~~~~↖(^ω^)↗              

    ------------------------ 没有谁的人生不是斩棘前行 ---------------------------------------- JM
  • 相关阅读:
    asp.net中Session过期设置方法
    SQL server的一道入门面试题背后的思考
    SQL Server 2008中SQL应用之-“阻塞(Blocking)”
    win2003+vs2010下安装asp.net MVC3正式版失败经历
    WinForm下ComboBox设定SelectedValue总结
    SQL Server 2008中的代码安全(四):主密钥
    【译】SQL Server误区30日谈Day12TempDB的文件数和需要和CPU数目保持一致
    西雅图SQL PASS之旅
    【译】SQL Server误区30日谈Day10数据库镜像在故障发生后,马上就能发现
    Ado.net的连接池
  • 原文地址:https://www.cnblogs.com/Lynn0814/p/4674496.html
Copyright © 2011-2022 走看看