2016 "Baidu Star"-Preliminary (Astar round2a) 1005 BD string (HDU5694)--Find the law, string symmetry, divide and conquer

Source: Internet
Author: User

 Analysis: According to the meaning given by the topic each processing of the new string is highly symmetric, for example, the title of the third string, the most middle is B then scattered on both sides, the other side of the center of the symmetry of the position must be D, and vice versa. So from any point, just find out his symmetry center, from the other side of the symmetry center to this point between them all the characters, to remove the symmetry center is B, the other letters, the number B and D must be equal (from the problem given in the transformation method known). So that's a simple question. This is about the symmetry center. The number of len,b to the length of its symmetrical point is len/2+1, because Len is obviously odd, so (len+1)/2 is no harm.

  we define DFS (x) to indicate how many b from the first to the X, so the answer from L to R is Dfs (r)-dfs (L-1). So now take this 7-length bbdbbdd to illustrate the Dfs method, if X is 7, it is at the edge of this symmetry interval, give directly (7+1)/2=4, but if it is 6? It is clear that the number of B is required from 1 to 6, about the fourth symmetry, the string is divided into B bdbbd D This way, the only use (5+1)/2 can only get 3, the reason is very simple, the first letter is not considered, then as long as the rest of the part of the recursion to do the same thing.

As to whether this position is at the boundary, we can simply preprocess the length of each string: 1,3,7,15 ... ... In this way, it is easy to know where x belongs to the symmetry zone, see the code:

1#include <stdio.h>2#include <algorithm>3#include <string.h>4#include <vector>5 using namespacestd;6typedefLong Longll;7 8Vector<ll>V;9 voidInit ()Ten { Onell x=1; A      while(x<=(LL) 1e18) -     { - v.push_back (x); thex=2*x+1; -     } - } -  +ll Dfs (intx) - { +     if(x==0)return 0; A     intY=lower_bound (V.begin (), V.end (), x)-v.begin ();//find out that x is contained within the symmetry zone . at     if(v[y]==x)return(x+1)/2;//if it is just at the edge of the symmetry interval, return directly, because there are no letters left to be processed . -ll t = v[y-1]+1;//Otherwise, it's obvious that the last position of the preceding string, plus 1, is the symmetry center of the symmetric interval where x is. -     returnx-t+1+dfs (x2* (X-T)-1);//divide and conquer the remaining parts - } -  - intMain () in { - init (); to     intT; +scanf"%d",&T); -      while(t--) the     { * ll L,r; $scanf"%i64d%i64d",&l,&R);Panax Notoginsengll ans = DFS (r)-dfs (l1); -printf"%i64d\n", ans); the     } +     return 0; A}

2016 "Baidu Star"-Preliminary (Astar round2a) 1005 BD string (HDU5694)--Find the law, string symmetry, divide and conquer

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.