QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#845194#1942. Another Substring Query ProblemCYYHTL 1806ms542652kbC++232.2kb2025-01-06 15:21:062025-01-06 15:21:13

Judging History

This is the latest submission verdict.

  • [2025-01-06 15:21:13]
  • Judged
  • Verdict: TL
  • Time: 1806ms
  • Memory: 542652kb
  • [2025-01-06 15:21:06]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define il inline
#define ptc putchar
#define pb push_back
#define R(i, l, r) for (int i = l; i <= r; ++i)
#define debug puts("--------------------------------------------")
typedef long long ll;
typedef pair<int, int> PII;
namespace ZT {
    template <typename T>
    il void read(T &x) {
        x = 0; T f = 1; char ch;
        while (!isdigit(ch = getchar())) f -= (ch == '-') << 1;
        while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
        x *= f;
    }
    template <typename T, typename ...L>
    il void read(T &x, L &...y) {read(x); read(y...);}
    template <typename T>
    il void write(T x) {
        if (x < 0) ptc('-'), x = -x;
        if (x > 9) write(x / 10);
        ptc(x % 10 + '0');
    }
    template <typename T, typename ...L>
    il void write(T &x, L &...y) {write(x), ptc(' '); write(y...);}
}
using namespace ZT;
const int N = 2e5 + 5, P = 1e9 + 7;
char s[N];
int n, q, k, B;
ll hsh[N], base[N] = {1};
char t[N];
void init() {
    R(i, 1, n) {
        base[i] = base[i - 1] * 10 % P;
        hsh[i] = (hsh[i - 1] * 10 % P + s[i] - 'a') % P;
    }
}
ll get(int l, int r) {
    return (hsh[r] - hsh[l - 1] * base[r - l + 1] % P + P) % P;
}
unordered_map <ll, vector <int> > str;
signed main() {
    scanf("%s", s + 1); n = strlen(s + 1); B = sqrt(n) * 3 / 2;
    init();
    R(i, 1, n) {
        R(j, 1, B) {
            if (i + j - 1 > n) break;
            ll v = get(i, i + j - 1);
            str[v].push_back(i);
        }
    }
    read(q);
    while (q--) {
        scanf("%s", t + 1); read(k); int m = strlen(t + 1);
        ll now = 0;
        R(i, 1, m) now = (now * 10 + t[i] - 'a') % P;
        if (m <= B) {
            if (str[now].size() < k) puts("-1");
            else write(str[now][k - 1]), ptc('\n');
        } else {
            R(i, 1, n - m + 1) {
                ll v = get(i, i + m - 1);
                if (v == now) --k;
                if (!k) {
                    write(i), ptc('\n');
                    break;
                }
            }
            if (k) puts("-1");
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1806ms
memory: 542652kb

input:

hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh...

output:

50997
112451
97534
13355
150562
5772
146250
10904
4343
194777
71395
96501
6809
18571
166901
114789
123587
176339
29660
82650
63509
80101
47907
66143
199971
108032
140518
49221
46212
28127
61461
149025
142818
33646
17132
196161
36671
28656
16860
141951
139429
72866
162807
21343
194803
16263
118701
20...

result:

ok 631 lines

Test #2:

score: -100
Time Limit Exceeded

input:

gnzlzbsarybaxilacuopvdkhjfiynrgfdhsrecyxedcrriztcxboczhfaseqglcwshaewaepasxqpaxxbroxndbxqabrvtrrshrnnlyfodtxyltjkbghfkjcqrqsuwdsgypcaroaojqbfrjqbdrimglxcqtfmqaeogqunaegfpfzcmyjwfgbwaigwitcyspiedbisjuedeyseneuzjdteifzvdfzhenapmmreqpvwsmtrxowjnmmrthzhojgxsoppfgfgohoghuwvqclzsmeydusfndrdyqokpqwupixyoxr...

output:


result: