luogu#P5212. SubString

    ID: 4185 远端评测题 1000~3000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>平衡树后缀自动机 SAMO2优化动态树 LCT

SubString

Problem Description

Given a string init, you need to support two operations:

  • Append a string to the end of the current string.

  • Query how many times a string ss appears in the current string (as a contiguous substring).

The operations are strictly online.

Input Format

The first line contains an integer QQ, the number of operations.

The second line contains a string, the initial string init.

Next come QQ lines. Each line contains two strings, Type and Str.

  • Type is ADD, meaning to append a string to the end.

  • Type is QUERY, meaning to ask how many times a string appears in the current string.

To enforce online processing, you need to maintain a variable mask, initially 00.

String decodeWithMask(String s, int mask) {
	char[] chars = s.toCharArray();
	for (int j = 0; j < chars.length; j++) {
		mask = (mask * 131 + j) % chars.length;
		
		char t = chars[j];
		chars[j] = chars[mask];
		chars[mask] = t;
	}
	
	return new String(chars);
}

After reading Str, use this process to decode it into the real string TrueStr.

For a query, query TrueStr and output one line with the answer Result.

Then update mask=maskResultmask = mask \bigoplus Result.

For an insertion, append TrueStr to the end of the current string.

Note: the strings in both ADD and QUERY operations must be decoded.

Output Format

For each QUERY operation, output how many times the queried string appears in the current string.

2
A
QUERY B
ADD BBABBBBAAB
0

Hint

init6×105|\mathrm{init}| \leq 6 \times 10^5, Q6×105Q \leq 6 \times 10^5, and the total length of all queried strings is at most 3×1063 \times 10^6.

It is guaranteed that the strings contain only A and B.

To avoid slow judging, for test points 2,3,5,6,8,112,3,5,6,8,11 the time limit is 3 s, and for the others it is 1 s.

Original problem: BZOJ 2555.

Translated by ChatGPT 5