习题5-5 复合词 UVa10391

1.题目描述:点击打开链接

2.解题思路:原来试了若干种思路,都失败了:1.枚举两个单词获得TLE(写了好几遍都是TLE==),2,从一个单词中拆分出一个已有单词查找剩余部分是否存在获得WA,最后决定枚举一个单词,将其拆分成两部分分别查找竟然通过了(⊙o⊙)

3.代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<cassert>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<functional>
using namespace std;
const int maxn = 120000 + 10;
set<string>word;
string wrd[maxn];

int main()
{
	int cnt = 0;
	string buf;
	while (cin >> buf)
	{
		word.insert(buf);
		wrd[cnt] = buf;
		cnt++;
	}
	for (int i = 0; i < cnt;i++)
	{
		string buf = wrd[i];
		int len = wrd[i].length();
		for (int j = 1; j < len; j++)//拆分单词,利用set进行查找
		{
			string pre = wrd[i].substr(0, j);
			string suf = wrd[i].substr(j, len - j);
			set<string>::iterator a,b;
			a = word.find(pre);
			b = word.find(suf);
			if (a != word.end() && b != word.end())
			{
				cout << wrd[i] << endl;
				break;
			}
		}
	}
	return 0;
}

你可能感兴趣的:(uva)