【c/c++】 机试中的 hash 问题

先看一个问题:要求先输入数目 n , 再输入 n 个数(取值范围 0 到 100),n = 0 时程序结束,再输入一个值 a, 输出 n 个数中与 a 相同的数字的个数。

面对这样的问题,很容易想到的是将输入的 n 个数先存储起来,然后 将输入的 n 与上述的 n 个数进行比较,cnt++ 的方式最终输出胡个数,这里介绍的是 hash 的方法,即在输入 n 个数的同时,就将它们对应的个数进行存储,Hash[x]存储的就是 x 的个数,最终只要输出输入的 a 的Hash[a] 即可。

代码为:

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;

int main()
{
	int n;
	//当 n 为0时程序结束,这是程序的条件
	while (scanf("%d", &n) != EOF && n != 0)
	{
		int Hash[101] = { 0 };
		for (int i = 0; i < n; i++)
		{
			int x;
			scanf("%d",&x);
			Hash[x]++;
		}
		int a;
		scanf("%d",&a);
		printf("%d",Hash[a]);
	}
	
	return 0;
}

运行结果为:

3
90 80 60
60
1
4
90 80 60 60
60
2
4
90 80 60 90
60
1

再看第二个例子:

题目描述:

给你 n 个整数,请按从大到小的顺序输出其中前 m 大的数。

输入:

每组测试数据有两行,第一行有两个数 n,m(0<n,m<1000000),第二行包含 n个各不相同,且都处于区间[-500000,500000]的整数。

输出:

对每组测试数据按从大到小的顺序输出前 m 大的数。

样例输入:

53

3 -35 92 213 -644

样例输出:

213 92 3

注意的是输入的 n 个数各不相同,要注意的是这里最好不能用 sort, 因为 n 最大可能取到1000000,快排的时间复杂度为O(nlogn) , 会超出一秒的时间限制,这是可以考虑 hash 这种方法,即以空间换时间,因为存储值以及遍历一遍的时间复杂度最多为O(1),这样可以大大提高程序的效率。

代码为:

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
//define后面不能加;会报错!!!这也是前面程序出现错误的原因
#define OFFSET 500000
//当分配较大数组时就在函数外定义,共有100001个值
int Hash[1000001];

int main()
{
	int n, m;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		//全部初始化为0,一会试试{0}的操作
		for (int i = -500000; i <= 500000; i++)			
			Hash[i + OFFSET] = 0;
		for (int i = 0; i < n; i++)
		{
			int x;
			scanf("%d",&x);
			//因为题目要求输入的 n 个数不能重复,所以用 0 和 1 就能存储下来
			Hash[x + OFFSET] = 1;
		}
		int cnt = 0;
		for (int i = 500000; i >= -500000, cnt != m; i--)
		{
			if (Hash[i + OFFSET] != 0)
			{
				printf("%d", i);
				cnt++;
				//当已经输出 m 个值时将会打印一个换行符
				//没有时则在每个新打印的值后增加一个空格,而不是再%d后面加上一个空格
				//在%d后加空格,最后一个数打印时可能会因为打印一个空格和一个换行出现错误
				//这一点以后在继续观察
				if (cnt == m)
				{
					printf("\n");
					break;
				}
				else
					printf(" ");
			}

		}
		
	}
	return 0;
}

运行结果:

6 2
100 300 400 10 20 301
400 301
5 3
3 -35 92 213 -644
213 92 3

 

好的,我可以为您提供一个基于SimHash算法的C++程序相似性检测系统的代码实现。SimHash算法是一种快速的文本相似性度量算法,它可以将文本表示成一个固定长度的二进制向量,并通过计算两个文本的哈希值的汉明距离来衡量它们的相似度。以下是代码实现: ```c++ #include <iostream> #include <string> #include <vector> #include <bitset> #include <algorithm> using namespace std; const int HASH_BITS = 64; // 定义哈希值的位数 const int K_GRAMS = 3; // 定义k-gram的长度 // 将一个字符串分成k-gram,并返回每个k-gram的哈希值 vector<uint64_t> getKGramHash(const string &s) { vector<uint64_t> hashes; for (int i = 0; i <= s.length() - K_GRAMS; i++) { uint64_t hash = 0; for (int j = 0; j < K_GRAMS; j++) { hash = (hash << 8) | s[i + j]; } hashes.push_back(hash); } return hashes; } // 计算SimHash值 bitset<HASH_BITS> getSimHash(const vector<uint64_t> &hashes) { vector<int> bits(HASH_BITS, 0); for (uint64_t hash : hashes) { for (int i = 0; i < HASH_BITS; i++) { if (hash & (1ull << i)) { bits[i]++; } else { bits[i]--; } } } bitset<HASH_BITS> simHash; for (int i = 0; i < HASH_BITS; i++) { simHash[i] = (bits[i] > 0); } return simHash; } // 计算两个SimHash值的汉明距离 int hammingDistance(const bitset<HASH_BITS> &a, const bitset<HASH_BITS> &b) { return (a ^ b).count(); } // 计算两个字符串的相似度 double similarity(const string &a, const string &b) { vector<uint64_t> aHashes = getKGramHash(a); vector<uint64_t> bHashes = getKGramHash(b); bitset<HASH_BITS> aSimHash = getSimHash(aHashes); bitset<HASH_BITS> bSimHash = getSimHash(bHashes); int distance = hammingDistance(aSimHash, bSimHash); return 100.0 - (double) distance * 100.0 / (double) HASH_BITS; } int main() { string a = "hello world"; string b = "hello world!"; string c = "goodbye world"; double similarityAB = similarity(a, b); double similarityAC = similarity(a, c); cout << "The similarity between a and b is: " << similarityAB << "%" << endl; cout << "The similarity between a and c is: " << similarityAC << "%" << endl; return 0; } ``` 这段代码实现了一个简单的C++程序相似性检测系统。它首先将输入的两个字符串分成k-gram,然后计算它们的SimHash值,并求出其汉明距离,最后用汉明距离计算相似度。您可以根据需要修改K_GRAMS和HASH_BITS的值来调整k-gram的长度和哈希值的位数,以达到更好的检测效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值