跳到正文

题解

CF644C

wizard_Marshall约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
获赞
1
题目
CF644C

版本

  1. v1当前

题意

给定若干个网站,分为主机名与路径。如果两台主机查询的路径都相同,它们就属于一个集合。求出所有集合。

网站给出的是如下格式:

代码
http://主机名/路径

注意题目强调主机名后可能不会有斜杠,这也算一种查询。

思路

首先肯定先想到把一个主机的所有路径记录下来然后去重(重复没有意义还会干扰判断)。之后通过 map 等数据结构找哪几个机子是相等的。可以用字符串哈希来做。

这里使用单哈希会爆,双哈希也会爆,字符串之间加了间隔等也会爆。只是数据点多少的问题。我们可以用 std 库中自带的哈希函数。

具体格式:

代码
hash<数据结构名>()(需要哈希的变量)

最后判断哈希值相同的是否多于一个即可输出。

代码

C++
#include <bits/stdc++.h>

using namespace std;
#define int long long
const int p = 998244353;
map <string, bool> is;//去重
map <string, vector <string> > mp;//每个主机后路径的集合
map <int, vector <string> > hsh;//同一个哈希值的主串集合
signed main() {
	ios::sync_with_stdio();
	cin.tie(0);cout.tie(0);
	int n;
	cin >>n;
	while (n--) {
		string s;
		cin >> s;

		if (is[s])continue;
		is[s] = 1;
		int len = s.size(), i;
		string name1 = "", name2 = "";
		for (i = 7; i < len; i++) {
			if (s[i] == '/')break;
			name1 += s[i];
		}
		for (; i < len; i++) {
			name2 += s[i];
		}
		mp[name1].push_back(name2);//name1是主机名,name2是路径
	}
	for (auto i : mp) {
		sort(i.second.begin(), i.second.end());
		string sum = "";
		for (auto j : i.second) {
			sum += j + '-';//字符串之间最好用-隔开,不然会出现诸如a-bc=ab-c的错误
		}
		hsh[hash<string>()(sum)].push_back(i.first);//哈希映照
	}
	int cnt = 0;
	for (auto i : hsh) {
		if (i.second.size() > 1) {//统计相同的个数
			cnt++;
		}
	}
	cout << cnt << endl;
	for (auto i : hsh) {
		if (i.second.size() <= 1)continue;
		for (auto j : i.second) {
			cout << "http://" << j << " ";//记录的时候不带http就要加上
		}cout << endl;
	}
	return 0;
}
原文