uva 10391 Compound Words <set>

本文介绍了一个简单的算法,用于从字典中找出所有由两个单词组成的复合词汇。输入为按字母顺序排列的单词列表,输出为同样按字母顺序排列的复合词汇列表。示例输入包括'a'、'alien'等单词,输出则包含'alien'和'newborn'这样的复合词汇。

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 theconcatenation 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, inalphabetical order.

Sample Input  

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra

Sample Output  

alien
newborn


用set做
#include <iostream>
#include <set>
#include <string>
using namespace std;

set<string>dic;
set<string>::iterator t;

int main()
{
    string word;
    while(cin >> word)dic.insert(word);
    
    for(t = dic.begin(); t != dic.end(); t++){
        word = *t;
        int n = word.length();

        for(int i = 0; i < n - 1; i++ ){
            string s1=word.substr(0,i+1),s2 = word.substr(i+1,n-i-1);
            if( (dic.count(s1)) && (dic.count(s2)) ){
                cout << word <<endl;
                break;
            }
        }
    }
    //system("pause");
    return 0;
}

 

 

dic.count(s2)  在set dic中计算s2的个数,并返回其个数。

 

转载于:https://www.cnblogs.com/farewell-farewell/p/5477433.html

SAMPLE语言定义 一、字符集定义 1.<字符集> → <字母>│<数字>│<单界符> 2.<字母> → A│B│…│Z│a│b│…│z 3.<数字> → 0│1│2│…│9 4.<单界符> → +│-│*│/│=│<│>│(│)│[│]│:│. │; │, │' 二、单词集定义 5.<单词集> → <保留字>│<双界符>│<标识符>│<常数>│<单界符> 6.<保留字> → and│array│begin│bool│call│case│char│constant│dim│do│else│end│false│for│if│input│integer│not│of│or│output│procedure│program│read│real│repeat│set│stop│then│to│true│until│var│while│write 7.<双界符> → <>│<=│>=│:= │/*│*/│.. 8.<标识符> → <字母>│<标识符> <数字>│<标识符> <字母> 9.<常数> → <整数>│<布尔常数>│<字符常数> 10.<整数> → <数字>│<整数> <数字> 11.<布尔常数> → true│false 12.<字符常数> → ' 除 {'} 外的任意字符串 ' 三、数据类型定义 13.<类型> → integer│bool│char 四、表达式定义 14.<表达式> → <算术表达式>│<布尔表达式>│<字符表达式> 15.<算术表达式> → <算术表达式> + <项>│<算术表达式> - <项>│<项> 16.<项> → <项> * <因子>│<项> / <因子>│<因子> 17.<因子> → <算术量>│- <因子> 18.<算术量> → <整数>│<标识符>│( <算术表达式> ) 19.<布尔表达式> → <布尔表达式> or <布尔项>│<布尔项> 20.<布尔项> → <布尔项> and <布因子>│<布因子> 21.<布因子> → <布尔量>│not <布因子> 22.<布尔量> → <布尔常量>│<标识符>│( <布尔表达式> )│ <标识符> <关系符> <标识符>│<算术表达式> <关系符> <算术表达式> 23.<关系符> → <│<>│<=│>=│>│= 24.<字符表达式> → <字符常数>│<标识符> 五、语句定义 25.<语句> → <赋值句>│<if句>│<while句>│<repeat句>│<复合句> 26.<赋值句> → <标识符> := <算术表达式> 27.<if句>→ if <布尔表达式> then <语句>│if <布尔表达式> then <语句> else <语句> 28.<while句> → while <布尔表达式> do <语句> 29.<repeat句> → repeat <语句> until <布尔表达式> 30.<复合句> → begin <语句表> end 31.<语句表> → <语句> ;<语句表>│<语句> 六、程序定义 32.<程序> → program <标识符> ;<变量说明> <复合语句> . 33.<变量说明> → var <变量定义>│ε 34.<变量定义> → <标识符表> :<类型> ;<变量定义>│<标识符表> :<类型> ; 35.<标识符表> → <标识符> ,<标识符表>│<标识符> 七、SIMPLE语言单词编码 单 词 种别码 单 词 种别码 单 词 种别码 and 1 output 21 * 41 array 2 procedure 22 */ 42 begin 3 program 23 + 43 bool 4 read 24 , 44 call 5 real 25 - 45 case 6 repeat 26 . 46 char 7 set 27 .. 47 constant 8 stop 28 / 48 dim 9 then 29 /* 49 do 10 to 30 : 50 else 11 true 31 := 51 end 12 until 32 ; 52 false 13 var 33 < 53 for 14 while 34 <= 54 if 15 write 35 <> 55 input 16 标识符 36 = 56 integer 17 整数 37 > 57 not 18 字符常数 38 >= 58 of 19 ( 39 [ 59 or 20 ) 40 ] 60
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值