UVA 10391复合词

题目大意 给定一个词典,要求找出所有的复合词,即恰好有两个单词连接而成的单词
分析
第一种方法 使用集合查看当前单词的分解手否存在

#include<iostream>
#include<algorithm>
#include<set>
#include<string>
#include<string.h>
#include<map>
#define maxn 120000+10
using namespace std;

string st[maxn];
int main()
{
	set<string>se;	
	int cur = 0;
	while (cin >> st[cur]) {
		se.insert(st[cur++]);
	}
	for (int i = 0; i < cur; i++) {
		for (int j = 0; j < st[i].length(); j++) {
			string str1 = st[i].substr(0, j);
			string str2 = st[i].substr(j);
			if (se.count(str1) && se.count(str2)) {
				cout << st[i] << endl; break;
			}
		}
	}
	return 0;

第二种方法使用哈希函数

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#define maxn 120000+10
#define maxh 500000
using namespace std;

int head[maxh], ne[maxn];
string st[maxn],word[maxn];
int cur,n;
void initial() {
	cur = 0;
	memset(head, -1, sizeof(head));
}
int Hash(string s) {				//获取表头;
	unsigned int seed = 31;
	unsigned int ha = 0;
	for (int i = 0; s[i] != 0; i++) {
		ha = ha * seed + (s[i]);   //为什么乘以奇数-避免冲突
	}
	return ha % maxh;
}

void add(string s) {
	int id = Hash(s);
	st[cur] = s;
	ne[cur] = head[id], head[id] = cur++;
}

bool find(string s) {
	int id = Hash(s);
	//if (head[id] == -1)return false;
	for (int i = head[id]; i != -1; i = ne[i])
		if (st[i] == s)
			return true;
	return false;
}

int main() {
	n = 0;
	initial();
	while (cin >> word[n])
		add(word[n++]);
	for (int i = 0; i < n; i++) {
		for (int j = 1; j < word[i].size(); j++) {
			string str1 = st[i].substr(0, j);
			string str2 = st[i].substr(j);
			if (find(str1) && find(str2)) {
				cout << word[i] << endl;
				break;
			}
		}
	}
	return 0;
}

第三种 也可以使用状态数组+map对应

#include<iostream>
#include<algorithm>
#include<set>
#include<string>
#include<string.h>
#include<map>
#define maxn 120000+10
using namespace std;
map<string, bool>  ha;

int main()
{
	int cur = 0;
	while (cin >> st[cur]) {
		ha[st[cur++]] = 1;
	}
	for (int i = 0; i < cur; i++) {
		for (int j = 1; j < st[i].size(); j++) {
			string str1 = st[i].substr(0, j);
			string str2 = st[i].substr(j);
			if (ha[str1] && ha[str2]) {
				cout << st[i] << endl;
				break;
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值