【模板】字符串哈希

D e s c r i p t i o n Description Description

如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字、大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串。

#友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转PJ试炼场:)

I n p u t Input Input

第一行包含一个整数N,为字符串的个数。

接下来N行每行包含一个字符串,为所提供的字符串。

O u t p u t Output Output

输出包含一行,包含一个整数,为不同的字符串个数。

S a m p l e I n p u t Sample Input SampleInput
5
abc
aaaa
abc
abcc
12345
S a m p l e O u t p u t Sample Output SampleOutput
4

思路

哈希
或暴力/笑

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
map<string,int>A;
int Ans,n;
string s;
int main()
{
	scanf("%d\n",&n);
	Ans=n;
	for(int i=1;i<=n;++i) 
	{
		getline(cin,s);
		if(A[s])Ans--;
		A[s]++;
	}
	printf("%d",Ans);
	return 0;
}
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef unsigned long long ull;
ull F[100250];
ull Base=131;
int Ans,n;
string s;
int main()
{
	scanf("%d\n",&n);
	Ans=n;
	for(int k=1;k<=n;++k) 
	{
		getline(cin,s);
		for(int i=1;i<=(int)s.length();++i)
			F[k]=(F[k]*Base+(ull)s[i-1])%Mod;
	}
	sort(F+1,F+n+1);
	for(int i=2;i<=n;++i)
		if(F[i]==F[i-1])Ans--;
	printf("%d",Ans);
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值