算法习题---5-5复合词(UVa10391)
一:题目
输入一系列由小写字母组成的单词。输入已按照字典序排序,且不超过120000个。找出所有的复合词,即恰好由两个单词连接而成的单词
(一)样例输入
a alien born less lien never nevertheless new newborn the zebra
(二)样例输出
alien
newborn
二:代码实现
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> #include <string> using namespace std; int main() { freopen("data5_5_h.in", "r", stdin); freopen("data5_5_h.out", "w", stdout); int pos; string word; vector<string> word_vec; while (cin >> word) //先获取信息 word_vec.push_back(word); for (int i = 1; i < word_vec.size(); i++) //开始检验 for (int j = i - 1; j >= 0; j--) //先检查前半部分 if ((pos = word_vec[i].find(word_vec[j])) != string::npos) for (int k = 0; k < word_vec.size(); k++) //检查后半部分 { if (k==j||k==i) continue; if (word_vec[i].find(word_vec[k], pos + word_vec[j].length()) != string::npos) { if ((pos + word_vec[k].length() + word_vec[j].length()) == word_vec[i].length()) cout << word_vec[i] << endl; } } freopen("CON", "r", stdin); freopen("CON", "w", stdout); return 0; }