首页 > 其他分享 >codeforces963D. Frequency of String【哈希】

codeforces963D. Frequency of String【哈希】

时间:2022-08-20 10:57:40浏览次数:59  
标签:hash codeforces963D int scanf 哈希 unsigned long Frequency str

我的腿让我停下,可是我的心却不许我这么做

今天又是为了明知多半不可能的事情奔波一早,一天里,出了很多丑,犯了很多错,见了很多人,有了很多意想不到的收获,我选择了我的生存方式,我努力地撒野生长。现在是凌晨一点了,我不敢去睡觉,因为夜幕总会揭开我的绝望,总是这样。武汉大学的校训是什么来着?自强弘毅?自强我懂,弘毅我不想懂。无聊地翻翻知网,现在已经能随意下载。图书馆里也有很多外文资料,那也是我唯一的栖居地了。我不理解为什么刚开学就要把自己搞得这么忙而碌碌,或许,我只是不想把能花精力解决的问题,变成需要时间接受的结局。接受每一步,就不能害怕接受;可为了前进,就不能甘心接受———那么我到底该怎样?一辈子活在过去的阴霾下,即使都麻木到看不透阴霾埋藏之物,在黑暗中独步行吟,仔细分析题意,发现不同的字符串长度最多只有\(\sqrt{n}\)个,所以可以按长度分类,而每种长度内部都是\(O(n)\)的,总复杂度即是\(n \sqrt{n}\)的。对每一长度中涉及的询问字符串,处理的方法就是暴力遍历,求出所有目标串的起始点(方法太多,这里姑且用\(hash\)解决,之后就显然了。

$click$ $for$ $codes$
# include "bits/stdc++.h"
using namespace std;
constexpr int N = 1e6 + 3;
unsigned long long h[N], b[N], h_2[N]; // array space also bothered me for a time. Actually, I still havn't figure out what's wrong……
int length[N], id[N], k[N];
vector<int> ans[N];
char str[N], q_str[N];
int main() {
//	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); // if we turn off the I / O synchronization stream and use both cin and scanf, it will cause a special error. Perhaps a space problem, I suppose 
	scanf("%s", str + 1);
	int len = strlen(str + 1);
	b[0] = 1;
	constexpr unsigned long long base = 1331; // hash pretreatment
	for(int i = 1; i <= len; ++i) {
		h[i] = h[i - 1] * base + str[i];
		b[i] = b[i - 1] * base;
	}
	int n;
	cin >> n;
	unordered_map<unsigned long long, int> mp;
	for(int i = 1; i <= n; ++i) {
		char q_str[len + 1];
		cin >> k[i];
		scanf("%s", q_str + 1);
		id[i] = i;
		length[i] = strlen(q_str + 1);
		for(int j = 1; j <= length[i]; ++j) {
			h_2[i] = h_2[i] * base + q_str[j]; // natural overflow // I wonder why some guy like persecuting others who prefer natural overflow, maybe they worked under Hitler in their previous lives :) 
		}
		mp[h_2[i]] = i;
	}
	sort(id + 1, id + n + 1, [&](int &x, int &y) {
		return length[x] < length[y]; // sort by length to classification
		/*
			if we write length[id[x]] < length[id[y]], there seems to be no differnce as id[i] is equal to i. However, as the original sequence changes gradually, this complex sentence no longer maintains its accurancy anymore
		*/
	});
	int i = 1;
	while(i <= n) {
		auto hash = [&](int l, int r) -> unsigned long long {
			return h[r] - h[l - 1] * b[r - l + 1]; // hash's magic, O(1) -> substring
		};
		for(int j = 1; j + length[id[i]] - 1 <= len; ++j) { // enumerating substring
			auto m = hash(j, j + length[id[i]] - 1);
			if(mp.find(m) != mp.end()) {
				ans[mp[m]].push_back(j); // put the starting position of each substring into the answer array
			}
		}
		while(i <= n && length[id[i]] == length[id[i + 1]]) ++i; // handle the same length case
		++i;
		if(i > n) break;
	}
	for(int i = 1; i <= n; ++i) {
		if(ans[i].size() < k[i]) {
			printf("-1\n");
		}
		else {
			int minn = 0x7fffffff;
			for(int j = k[i] - 1; j < ans[i].size(); ++j) { // considering the input mode of vector, the subscript is reduced by one
				minn = min(minn, ans[i][j] - ans[i][j - k[i] + 1] + length[i]); // enumerating calculation minimum length
			}
			cout << minn << "\n";
		}
	}
	return 0;
}

标签:hash,codeforces963D,int,scanf,哈希,unsigned,long,Frequency,str
From: https://www.cnblogs.com/bingoyes/p/16607042.html

相关文章

  • 哈希
    字符串哈希哈希是一种高效判断字符串相等的算法,准确来说是子串,因为预处理需要\(O(n)\),自然是多次使用才划算常见的哈希式是\(\suma_ibase^i(mod\mod)\)即选取一个......
  • 哈希类型,列表类型,集合类型,有序集合类型,慢查询,pipline与事务,发布订阅,Bitmap,HyperLogLog
    1API的使用1.1哈希类型###1---hget,hset,hdelhgetkeyfield#获取hashkey对应的field的value时间复杂度为o(1)hsetkeyfieldvalue#设置hashkey对应的field......
  • 哈希表4:两数之和(1)
    本题如下:(链接:https://leetcode.cn/problems/two-sum/)题目:给定一个整数数组nums 和一个整数目标值target,请你在该数组中找出和为目标值target 的那 两个 整数,并返......
  • Redis---hash哈希散列
    1.前言Redishash(哈希散列)是由字符类型的field(字段)和value组成的哈希映射表结构(也称散列表),它非常类似于表格结构。在hash类型中,field与value一一对应,且不允许重......
  • 子数组异或和(前缀和、哈希)
    题意给定一个长度为\(n\)的整数数组\(a_1,a_2,\dots,a_n\)。请你统计一共有多少个数组\(a\)的非空连续子数组能够同时满足以下所有条件:该连续子数组的长度为偶数。......
  • 哈希表2:两个数组的交集(349)
    本题如下:(链接:https://leetcode.cn/problems/intersection-of-two-arrays/submissions/)题目:给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一......
  • 道长的算法笔记:经典哈希表问题
    (一)哈希表简述Waiting...(二)使用哈希表优化复杂度(2.1)两数之和Waiting...(2.2)子数组异或和#include<bits/stdc++.h>#include<algorithm>usingnamespace......