UVA 10391 - Compound Words

题目大意:给定单词集合S,包含若干单词,找出S中所有满足这样条件的元素p:p == str1 + str2 && str1属于S && str2属于S


解题思路:用set的查找函数

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

int main() {
	string s;
	set <string> word;
	while (cin >> s)
		word.insert(s);
	for (set <string>::const_iterator it = word.begin(); it != word.end(); it++) 
		for (int i = 0, len = it->length(); i < len; i++)
			if (word.find(it->substr(0, i)) != word.end() && word.find(it->substr(i, len - i)) != word.end()) {
				cout << *it << endl;
				break;
			}
		return 0;
}



你可能感兴趣的:(UVA 10391 - Compound Words)