Title Link: Http://codeforces.com/problemset/problem/712/B
Test instructions
A person standing at the coordinates origin, he can move up (U), Down (D), left (L), right (R), four direction, now gives you a moving sequence, in order for him to finally return to the origin, you need to make some changes to the sequence, each time you can change one of the letters, ask the fewest number of changes.
Ideas:
If the length of this sequence is odd, then it is not possible to return to the origin, then directly output "1", otherwise you can think so, if "U" and "D" and "L" and "R" can be paired (and the order is not related), then you can definitely go back to the origin, so what needs to be done here is to , subtracting the horizontal direction, subtracting the vertical direction, means removing the logarithm of the paired occurrence. If the result is not 0, then need to make a change, it can be thought that the subtraction of the difference in addition to 2 can be the minimum number of changes.
Code:
#include <bits/stdc++.h>using namespaceStd;typedefLong LongLL;Const intMAXN =100000;intMain () {//freopen ("Input", "R", stdin);Ios_base::sync_with_stdio (); Cin.tie (); CharS[MAXN +3], C;intLen =0; while((c = GetChar ())! ='\ n') s[len++] =C; Map<Char,int>MP; mp['U'] = mp['D'] = mp['L'] = mp['R'] =0; for(inti =0; i < Len; i++) Mp[s[i]] + +;//count the occurrences of each operation intLR = ABS (mp['L']-mp['R']), du = ABS (mp['D']-mp['U']); if(Len &1) cout <<"-1"<<Endl; Elsecout << (lr + du) >>1) <<Endl; return 0;}
Codeforces 712B. Memory and Trident