問題一覧 > 通常問題

No.515 Typical LCP

レベル : / AI推定難易度 : 1625 / 実行時間制限 : 1ケース 1.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 77
作問者 : koba-e964 / テスター : ei1333333
お気に入りにしたユーザー ProblemId : 1408 / 自分の提出
問題文最終更新日: 2026-06-26 18:42:06
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 163 (順位表) の他の問題:

Problem Statement

There is an array of strings $s_1,\cdots,s_N$. Answer $M$ queries of the following form:
"For integers $i,j$ satisfying $1\le i < j \le N$, find the length of the longest common prefix of the strings $s_i,s_j$."
The $M$ queries are generated from the seeds x,d by the following method. Read it carefully and implement it.

for k in 1 .. M
  i[k] = (x / (n - 1)) + 1
  j[k] = (x % (n - 1)) + 1
  if (i[k] > j[k])
    swap(i[k], j[k])
  else
    j[k] = j[k] + 1
  end
  x = (x + d) % (n * (n - 1))
end
Here, the calculations are assumed to be performed using arbitrary-precision integers, and the division "/" returns the integer part of the quotient when regarded as real numbers.

Input

$N$
$s_1$
$\vdots$
$s_N$
$M$ $x$ $d$

(Corrected on 2017/5/5 22:46: the minimum value of N was changed from 1 to 2)
$2 \le N \le 100000 = 10^5$
$|s_i| \ge 1$ for all $1 \le i \le N$
$\sum_{i} |s_i| \le 800000 = 8 \times 10^5$
Each $s_i$ consists of lowercase Latin letters ('a' to 'z').
$1 \le M \le 3000000 = 3 \times 10^6$
$0 \le x < N(N - 1)$
$0 \le d < N(N - 1)$
$x$,$d$ are integers.

Output

For each $k$, compute the length of the longest common prefix of $s_{i_k}$ and $s_{j_k}$, and output the sum of these lengths on one line, followed by a newline.

Sample

Sample 1
Input
4
kinesis
koba
variance
kobushu
3 1 2
Output
4

In the first query, $x=1$, so $i=1, j=3$. The LCP of "kinesis" and "variance" is "", so its length is 0.
In the second query, $x=3$, so $i=1,j=2$. The LCP of "kinesis" and "koba" is "k", so its length is 1.
In the third query, $x=5$, so $i=2,j=4$. The LCP of "koba" and "kobushu" is "kob", so its length is 3.
The final output is their sum, 4.

Sample 2
Input
5
aaaaa
dadad
aaaab
abcbc
aaaac
4 1 1
Output
9

The results of the queries are 4,1,4,0, so output their sum, 9.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。