← 返回 snapchat 的题目列表IP Address Transformation / Increment-with-carry Variant ("One Step Dance" variant)
类型:online_judge
Problem: IPv4 String Transformation Variant (with carry + range constraint)
You are given IPv4 address strings in the form "a.b.c.d", where each segment is a decimal integer.
Define an operation similar to an “increment with carry” on IPv4:
Treat the IPv4 as 4 base-256 digits (each in [0,255]).
Increment the lowest segment by 1.
If a segment becomes 256, set it to 0 and carry 1 to the next higher segment (e.g., 0.0.0.255 -> 0.0.1.0).
In addition, you are given a numeric range [start, end] (both IPv4 strings). During the process, you may only generate/visit IPs that lie within the inclusive range [start, end], where comparison is by IPv4 numeric value (i.e., interpret a.b.c.d as a 32-bit integer).
Tasks:
Parse IPv4 strings to numeric form and convert back to string form.
Implement the above increment-with-carry under the range constraint.
(If required) Compute the minimum number of operations to reach end starting from start; return -1 if impossible.
Input
start: IPv4 string
end: IPv4 string
Output
An integer: minimum number of operations (or -1 if unreachable)
Example
Input: start = "0.0.0.255", end = "0.0.1.0"
Output: 1
Input: start = "0.0.0.1", end = "0.0.0.0"
Output: -1
Example
Input
0.0.0.255
0.0.1.0
Output
1