题目描述
如题,给定 NN 个字符串(第 ii 个字符串长度为 M_iMi,字符串内包含数字、大小写字母,大小写敏感),请求出 NN 个字符串中共有多少个不同的字符串。
友情提醒:如果真的想好好练习哈希的话,请自觉。
输入格式
第一行包含一个整数 NN,为字符串的个数。
接下来 NN 行每行包含一个字符串,为所提供的字符串。
输出格式
输出包含一行,包含一个整数,为不同的字符串个数。
输入输出样例
输入
5 abc aaaa abc abcc 12345输出
4
#include<iostream>
#include<string>
#include<vector>
#define MAXN 1510
#define base 261
#define mod 23333
using namespace std;
int n, ans;
char s[MAXN];
vector<string>linker[mod + 2];
inline void insert() {
int hash = 1;
for (int i = 0;s[i];i++)
hash = (hash * 111 * base + s[i]) % mod;//计算出字符串的hash值
string t = s;
for (int i = 0;i < linker[hash].size();i++) //遍历hash值为当前字符串hash值的hash链表,检查这个字符串是否存在
if (linker[hash][i] == t)
return; //如果找到一个同样的字符串 那么新的字符串则不进入答案
linker[hash].push_back(t);
ans++;
}
int main() {
cin >> n;
for (int i = 1;i <= n;i++)
cin >> s, insert();
cout << ans << endl;
return 0;
}