QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#845615#1942. Another Substring Query ProblemyqrTL 0ms0kbC++231.7kb2025-01-06 17:25:182025-01-06 17:25:20

Judging History

This is the latest submission verdict.

  • [2025-01-06 17:25:20]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 0kb
  • [2025-01-06 17:25:18]
  • 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, mod = 1e9 + 9;
ull pows[maxn];
string s, t[maxn];
int n, q, rk[maxn];
unordered_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] % mod + b.val) % mod);
	}
}h[maxn], H[maxn];
ull query(int l, int r) {return (H[r].val + mod - H[l - 1].val * pows[r - l + 1] % mod) % mod;}
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 <= n; i++) H[i] = H[i - 1] + Hash(s[i]);

	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());
	for(int i = 1; i <= n; i++) for(int j : lens) if(i + j - 1 <= n)
		app[query(i, i + j - 1)].push_back(i);//, printf("%d,%d\n", i, j);
	for(int i = 1; i <= q; i++)
	{
		ull val = Hash(t[i]).val;
		if(app.find(val) == app.end())
		{
			puts("-1");
			continue;
		}
		vector<int> &p = app[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: