Codeforces 712B. Memory and Trident

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.