#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <hash_map>
#include <algorithm>
using namespace std;
/*
问题:给定一组单词,编写一个程序,找出其中的最长单词,且该单词由这组单词中的其他单词组合而成。
分析:请弄清楚被组成的单词也是来自于当前词组中的。
将单词数组按照长度从长到短排序,对每个单词word,以序号i拆分成左右两个字符串,
对于左边的字符串leftWord=word[0...i],判断该字符串是否在单词数组中找到,对于右边的字符串
rightWord=word[i+1...length-1],递归调用判断是否是由其他单词组成
输入:
8(单词数组中元素个数n)
it bed side bedroom room a long alongside(n个单词)
输出
alongside(最长的单词)
关键:
1 请弄清楚被组成的单词也是来自于当前词组中的。
将单词数组按照长度从长到短排序,对每个单词word,以序号i拆分成左右两个字符串,
对于左边的字符串leftWord=word[0...i],判断该字符串是否在单词数组中找到,对于右边的字符串
rightWord=word[i+1...length-1],递归调用判断是否是由其他单词组成
2
//判断某个单词能否由单词数组中除自己以外的其他单词组成
bool canBeBuildWord(string& word , vector<string>& words , bool isOriginal, hash_map<string,bool> resultM
程序员面试金典——解题总结: 9.18高难度题 18.7给定一组单词,编写一个程序,找出其中的最长单词,且该单词由这组单词中的其他单词组合而成。
最新推荐文章于 2022-07-13 23:41:45 发布