Hash TLE?
查看原帖
Hash TLE?
638148
liujiaxi123456楼主2024/1/6 17:01
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int Maxn = 2e5+5;

namespace Josh_zmf {
	
	ull base1[Maxn], h1[Maxn], base2[Maxn], h2[Maxn];
	map <pair<ull, ull>, bool> bj;
	
	#define hash1(l, r)	(h1[r]-h1[l-1]*base1[r-l+1])
	#define hash2(l, r)	(h2[r]-h2[l-1]*base2[r-l+1])
	
	int main() {
		ios::sync_with_stdio(false);
		cin.tie(0), cout.tie(0);
		base1[1] = 29, base2[1] = 31;
		for(int i=2; i<=Maxn-5; i++)	base1[i] = base1[i-1]*base1[1];
		for(int i=2; i<=Maxn-5; i++)	base2[i] = base2[i-1]*base2[1];
		ll ans = 0, len = 0; ull pos1, pos2;
		string s;
		int T; cin>> T;
		getline(cin, s);
		while(T--) {
			ans = 0, bj.clear();
			getline(cin, s);
			len = s.size();
			for(int i=0; i<len; i++)	h1[i+1] = h1[i]*base1[1] + s[i];
			for(int i=0; i<len; i++)	h2[i+1] = h2[i]*base2[1] + s[i];
			for(int i=1; i<=len; i++) {
				for(int j=i; j<=len; j++) {
					pos1 = hash1(i, j), pos2 = hash2(i, j);
					if(!bj[make_pair(pos1, pos2)])	bj[make_pair(pos1, pos2)] = 1, ans++;
				}
			}
			cout<< ans<< '\n';
		}
		return 0;
	}
	
}

int main() {
	Josh_zmf::main();
	return 0;
}
2024/1/6 17:01
加载中...