习题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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值