8 analog solution to a problem

A. Matching

$ Hash $ directly engage.

If you use the $ KMP $, pay attention to B and A string exact match, this is a problem $ KMP $ can not handle.

 

 

B. home

At first I thought it was a cut point, so after careful thought found wrong.

Then hit a tree round side, no skills.

Another simple approach:

Analyzing only cut point, but modified method of determining cut points.

 1 void tarjan(int x)
 2 {
 3     int son=0;
 4     dfn[x]=low[x]=++num;
 5     for(register int i=fr[x];i;i=mo[i].pr)
 6     {
 7         register int y=mo[i].to;
 8         if(!dfn[y])
 9         {
10             tarjan(y);
11             low[x]=min(low[x],low[y]);
12             if(low[y]>=dfn[x]&&dfn[y]<=dfn[n])
13             {
14                 son++;
15                 if(x!=1||son>1) cut[x]=1;
16             }
17         }
18         else low[x]=min(low[x],dfn[y]);
19     }
20 }
From starsing

 

 

 

 

C. sushi

Into a chain-off ring, i.e., the 1 ~ n-1 added after the length of a string of n.

The problem in the R and B into 0 and 1,

Problems taken into a segment of length n, a movement, so that all the left and right sides of a distribution range.

1 each exchange should be easy to find and 0 in order to ensure optimal results.

If the 1 to the left, the moving distance equal to the number of both the intermediate and the distance from a left end point minus.

And optimizing the use of prefixes, so the equation can be introduced $ O (1) $ calculated.

Can $ O (n ^ 2) $ enumeration interval left and right dividing line 1, takes a minimum value.

 

About the enumeration process optimization:

For difficult to think of each interval, the dividing line is a single-function value for the valley. (Proof omitted, reference may prove positive Solution O (n))

Use the rule of thirds seeking extreme value can be unimodal function.

Note on the integer field, the rule of thirds can only shrink to the extreme value of the interval length of no more than 3, should try to update.

Complexity $ O (nlogn) $ constant slightly larger.

Also found with the right, optimum decision point range is not monotonic the left.

And the optimal decision point is a fixed value, the number 0 is set interval t,

If t is odd, optimal decision about the median point 0.

If t is even, optimal decision point in a median of about two to zero.

Proof :( Great God YXS )

To both sides of each movement distance is equivalent to a number between 1 and 0 in the left and right endpoint.

For each of a 1, when it is left in the position 0, the point to the left and more preferably,

Point to the right and vice versa better.

Since the total number of the interval 0 unchanged.

When the interval to the right,

If the original left point 1, the median 0 unchanged.

If the original left point 0, 0 in the right place.

Therefore, bit 0 monotonously changes, a pointer to the record bit position 0, with the $ $ is determined whether the right to the while.

Constantly updated answer, the total complexity $ O (n) $

 

Guess you like

Origin www.cnblogs.com/skyh/p/11246385.html