The islands of Polylogonia and Quasilogonia have mountain ranges
whose profiles are made of slopes at
.
A profile is described from left to right by a string of
“U” (uphill) and “D” (downhill) characters.
The two profiles have the same length. Each mountain range starts and
ends at sea level and never goes below it. Since it starts and ends at
sea level, each profile has the same number of “U” and
“D” characters. The figure below shows the profile
“UUDUUDUDDD”.
The Island Contour Planning Company (ICPC) owns a machine that is
capable of filling valleys in a very specific way. In one operation, it
chooses an island and covers an entire uphill slope with earth. This
slope runs from the bottom of a valley to the first peak to its right.
More precisely, the operation replaces a substring of that island’s
profile of the form
“”
()
with
“”,
where
“”
denotes
consecutive “U” characters. All other characters remain
unchanged. The machine cannot remove earth, only add it in the way just
described.
The figure below shows one operation of the machine: the part “” marked by the brace on the left becomes “” on the right, where the hatched strip is the earth that was added.
The ICPC wants to make the two profiles identical using its machine. It can be shown that this is always possible. Find the minimum total number of operations needed.
The first line contains a string (), representing the profile of Polylogonia.
The second line contains a string (), representing the profile of Quasilogonia.
Each string is made of the uppercase letters “U” and
“D”, indicating respectively uphill and downhill sections
of the corresponding mountain range. In each string the number of
“U” and “D” characters is the same, and no
prefix has more “D” characters than “U”
characters.
Output a single line with an integer indicating the minimum total number of operations needed to transform the profiles and into the same profile.
UUDDUD UUUDDD
2
The profiles of the two islands are depicted below.
Q has no “D”
followed by “U”, so no operation can be applied to it,
which means that both islands must end up with the profile
“UUUDDD”. P can
be turned into this profile with two operations, as shown below.
UUDDUUUDUUDDDD UUDUUDDUUUDDDD
4
Both P and Q can be turned into the profile
UUDUUUUUDDDDDD with two operations each, for a total of
four operations, as shown below.
UDUUDUUUDDDD UDUUUDDUUDDD
3