#P10646. [NordicOI 2023] ChatNOI

[NordicOI 2023] ChatNOI

Background

Translated from NordicOI 2023 Problem A ChatNOI.

Problem Description

You are playing a dialogue game with your robot. The rules are as follows.

First, you are given an array ww of nn strings (all strings contain only lowercase letters) and an integer kk.

Define the quality of an array of strings as the minimum, over every consecutive segment of length k+1k+1, of the number of times that segment occurs in ww.

There are qq queries. In each query, you are given mim_i and kk strings.

You need to construct mim_i strings so that, after appending these mim_i constructed strings one by one after the given kk strings, the resulting array of strings has the maximum possible quality.

If there are multiple solutions, output any one of them.

Input Format

The first line contains two integers n(1n5×105)n(1 \leq n \leq 5 \times 10^5) and k(1k<n)k (1 \leq k < n).

The second line contains a sentence of nn words wi(1wi10)w_i(1 \leq |w_i| \leq 10).

The third line contains an integer q(1q5×105)q(1 \leq q \leq 5 \times 10^5), meaning there are qq queries in total.

Then follow qq lines. Each line contains an integer mim_i and a substring of ss of length kk.

Output Format

For each query, you need to output the mim_i words after the given kk words.

13 2
ullen dullen doff kikke lane koff koffe lane bikke bane ullen dullen doff
3
1 ullen dullen
2 ullen dullen
3 ullen dullen
doff 
doff kikke 
doff kikke lane 
8 1
buffalo buffalo buffalo buffalo buffalo buffalo buffalo buffalo
1
7 buffalo
buffalo buffalo buffalo buffalo buffalo buffalo buffalo 
16 1
have you not heard about the bird the bird bird bird the bird is the word
8
1 have
1 you
1 not
1 heard
1 about
1 the
1 bird
1 is
you 
not 
heard 
about 
the 
bird 
bird 
the 

Hint

This problem uses bundled testdata.

Let M=miM = \sum m_i.

  • Subtask 1 (5 points): k<n100k < n \leq 100, k=1k = 1, 1q1001 \leq q \leq 100, mi=1m_i = 1.
  • Subtask 2 (7 points): k<n5×105k < n \leq 5 \times 10^5, 1k101 \leq k \leq 10, 1q1051 \leq q \leq 10^5, mi=1m_i = 1.
  • Subtask 3 (17 points): k<n6k < n \leq 6, 1k101 \leq k \leq 10, 1q101 \leq q \leq 10, 1mi61 \leq m_i \leq 6.
  • Subtask 4 (18 points): k<n5000k < n \leq 5000, 1k101 \leq k \leq 10, 1q50001 \leq q \leq 5000, qM5000q \leq M \leq 5000.
  • Subtask 5 (24 points): k<n5×105k < n \leq 5 \times 10^5, 1k101 \leq k \leq 10, 1q101 \leq q \leq 10, qM5×105q \leq M \leq 5 \times 10^5.
  • Subtask 6 (16 points): k<n105k < n \leq 10^5, 1k101 \leq k \leq 10, 1q1051 \leq q \leq 10^5, qM5×105q \leq M \leq 5 \times 10^5.
  • Subtask 7 (13 points): No special constraints.

For all testdata, k<n5×105k < n \leq 5 \times 10^5, 1k101 \leq k \leq 10, 1q1051 \leq q \leq 10^5, qM5×105q \leq M \leq 5 \times 10^5.

Translated by ChatGPT 5