QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#845596#1942. Another Substring Query ProblemyqrTL 0ms0kbC++232.1kb2025-01-06 17:15:352025-01-06 17:15:35

Judging History

This is the latest submission verdict.

  • [2025-01-06 17:15:35]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 0kb
  • [2025-01-06 17:15:35]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
constexpr int maxn = 200005;
constexpr ull base = 97;
ull pows[maxn];
string s, t[maxn];
int n, q, rk[maxn];
map<ull, vector<int> > app;
vector<int> lens;
struct Hash {
	int len;
	ull val;
	Hash() {len = val = 0;}
	Hash(const char &a) {len = 1, val = a - 'a' + 1;}
	Hash(const int &len, const ull &val) {this->len = len, this->val = val;}
	Hash(const string &str)
	{
		len = val = 0;
		for(char c : str) *this = *this + Hash(c);
	}
	friend Hash operator + (const Hash &a, const Hash &b) {
		return Hash(a.len + b.len, a.val * pows[b.len] + b.val);
	}
}h[maxn];
struct segment_tree {
	Hash s[maxn << 2];
	void pushup(int k) {s[k] = s[k << 1] + s[k << 1 | 1];}
	void build(int k, int sl, int sr)
	{
		if(sl == sr) return void(s[k] = Hash(::s[sl]));
		int mid = sl + sr >> 1;
		build(k << 1, sl, mid), build(k << 1 | 1, mid + 1, sr);
		pushup(k);
	}
	Hash query(int k, int sl, int sr, int ql, int qr)
	{
		if(ql <= sl && sr <= qr) return s[k];
		int mid = sl + sr >> 1;
		Hash ret(0, 0);
		if(ql <= mid) ret = query(k << 1, sl, mid, ql, qr);
		if(qr > mid) ret = ret + query(k << 1 | 1, mid + 1, sr, ql, qr);
		return ret;
	}
}tree;
int main()
{
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	pows[0] = 1;
	for(int i = 1; i < maxn; i++) pows[i] = pows[i - 1] * base;
	cin >> s >> q, n = s.size(), s = ' ' + s;
	for(int i = 1; i <= q; i++) cin >> t[i] >> rk[i], lens.push_back(t[i].size());
	sort(lens.begin(), lens.end()), lens.erase(unique(lens.begin(), lens.end()), lens.end());
	tree.build(1, 1, n);
	for(int i = 1; i <= n; i++) for(int j : lens) if(i + j - 1 <= n)
		app[tree.query(1, 1, n, i, i + j - 1).val].push_back(i);//, printf("%d,%d\n", i, j);
	for(int i = 1; i <= q; i++)
	{
		vector<int> &p = app[Hash(t[i]).val];
		// for(int j : p) printf("%d ", j);
		// puts("");
		if(p.size() < rk[i]) puts("-1");
		else printf("%d\n", p[rk[i] - 1]);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh...

output:


result: