算法与数据结构实验题 9.1 哈希

1、题目:

2、解题思路:

将每个子字符串中字母转换成数字,子串的数值作为数组的下标,只要两个数组的数组下标相同,则说明这两个子串相同,计数时不再加一。

3、代码:

#include<stdio.h>
#include<cmath>
#include<string>
int count[1000000]={0};
char str[1000000];
int main()
{
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	scanf("%s",str);
	int i,j=0;
	int value[1000];
	int counts=0;
	for(i=0;i<n-k+1;i++)
	{
		int h=k,ans=0;
		for(j=0;j<k;j++)
		{
			value[str[i+j]]=str[i+j]-96;
			ans+=value[str[i+j]]*pow(10,h-1);
			h--;
		}
		if(count[ans]==0)
		{
			count[ans]++;
			counts++;
		}
	}
	printf("%d
",counts);
	return 0;
}
原文地址:https://www.cnblogs.com/laixiaolian/p/6015135.html