luogu#P5212. SubString
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 appears in the current string (as a contiguous substring).
The operations are strictly online.
Input Format
The first line contains an integer , the number of operations.
The second line contains a string, the initial string init.
Next come lines. Each line contains two strings, Type and Str.
-
TypeisADD, meaning to append a string to the end. -
TypeisQUERY, 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 .
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 .
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
, , and the total length of all queried strings is at most .
It is guaranteed that the strings contain only A and B.
To avoid slow judging, for test points the time limit is 3 s, and for the others it is 1 s.
Original problem: BZOJ 2555.
Translated by ChatGPT 5