CF1705 C, D Solutions
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
C. Mark and His Unfinished Essay
Approach
Given these constraints, we obviously cannot actually copy the string, so we need to find another method.
We can see that every segment newly appended to the end of the string has an identical counterpart earlier in the string, located by an offset.
For example, consider the final insertion in the first sample:
If every letter in this is moved 9 positions backward, we find another , as follows:
Therefore, we can maintain a triple indicating that the characters in the interval are completely identical to those in the interval .
Then, whenever querying position , we can continually subtract the corresponding until lies within the range of the initial string.
Here is some further explanation.
Code
1 | // ttzytt |
D. Mark and Lightbulbs
Approach
First, let us simulate the fourth sample:
1 | 000101 |
Note: The positions marked in red indicate changes.
We can see that during this process, we can only lengthen or shorten a segment consisting of s, such as or (of course, viewed the other way around, it can be described as a segment consisting of s), rather than creating a new “ segment” out of thin air. This is because only when a changes to a , or a changes to a , will and differ, allowing us to change .
Therefore, if strings and have different numbers of segments, transforming into must be impossible.
We can see that in each operation, we can move the beginning or end of a “ segment” by one position. We can therefore use this fact to calculate the number of steps required to transform into .
That is, for every segment in and , we calculate the positions at which the segment begins and ends, then calculate the differences between corresponding segment endpoints in and . The sum of these differences is the answer.
How do we determine the beginning and end of a segment? They are simply where changes to and where changes to . Therefore, we create two arrays, and . After reading and , we traverse the two strings. Whenever , we put into (and do the same for and ). In this way, and store all segment endpoints in the two strings.
Code:
1 |
|






![[Stanford CS144] Lab 4 Record](/img/CS144/tcp%E7%8A%B6%E6%80%81%E6%B5%81%E8%BD%AC%E5%9B%BE.jpg)