25.md (7791B)
1 Source: https://codeforces.com/blog/entry/594 2 3 ### Problem A - IQ Test 4 5 We can store two values, *count**odd* and *count**even*, as the number of odd or even elements in the series. We can also store *last**odd* and *last**even* as the index of the last odd/even item encountered. If only one odd number appears --- output *last**odd*; otherwise only one even number appears, so output *last**even*. 6 7 8 9 ### Problem B - Telephone Numbers 10 11 There are many ways of separating the string into clusters of 2 or 3 characters. One easy way is to output 2 characters at a time, until you have only 2 or 3 characters remaining. Here is a possible C++ solution: 12 13 <code> 14 for( i=0; i<n; i++ ) 15 { 16 putchar(buf[i]); 17 if( i%2 && i<n-(n%2)-2 ) putchar('-'); 18 } 19 </code> 20 21 22 23 ### Problem C - Roads in Berland 24 25 If you are familiar with the [Floyd-Warshall algorithm](https://en.wikipedia.org/wiki/Floyd-Warshall_algorithm), then this solution may be easier to see. 26 27 Initially, we are given a matrix *D*, where *D*[*i*][*j*] is the distance of shortest path between city *i* and city *j*. Suppose we build a new road between *a* and *b* with length shorter than *D*[*a*][*b*]. How do we update the rest of the graph accordingly? 28 29 Define a new matrix *D*', whose entries *D*'[*i*][*j*] are the minimum path distance between *i* and *j* while taking into account the new road *ab*. There are three possibilities for each *i*, *j*: 30 31 * *D*'[*i*][*j*] remains unchanged by the new road. In this case *D*'[*i*][*j*] = *D*[*i*][*j*] 32 * *D*'[*i*][*j*] is shorter if we use the new road *ab*. This means that the new path *i*, *v*1, *v*2, ..., *v**n*, *j* must include the road *a*, *b*. If we connect the vertices *i*, *a*, *b*, *j* together in a path, then our new distance will be *D*[*i*][*a*] + *length*(*ab*) + *D*[*b*][*j*]. 33 * Lastly, we may have to use the road *ba*. (Note that this may *not* be the same as road *ab*.) In this case, we have *D*'[*i*][*j*] = *D*[*i*][*b*] + *length*(*ab*) + *D*[*a*][*j*]. 34 35 Thus, for each new road that we build, we must update each path *i*, *j* within the graph. Then we must sum shortest distances between cities. Updating the matrix and summing the total distance are both *O*(*N*2), so about 3002 operations. Lastly, there are at most 300 roads, so in total there are about 3003 operations. 36 37 One thing to note is that the sum of all shortest distances between cities may be larger than an int; thus, we need to use a long when calculating the sum. 38 39 40 41 ### Problem D - Roads not only in Berland 42 43 Before we start this problem, it is helpful to know about the [union find](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) data structure. The main idea is this: given some elements *x*1, *x*2, *x*3, ..., *x**n* that are partitioned in some way, we want to be able to do the following: 44 45 * merge any two sets together quickly 46 * find the parent set of any *x**i* 47 48 This is a general data structure that sometimes appears in programming competitions. There are a lot of ways to implement it; one good example is written by Bruce Merry (aka BMerry) [here](https://www.google.com/url?sa=t&source=web&cd=7&ved=0CC8QFjAG&url=http%3A%2F%2Folympiad.cs.uct.ac.za%2Fpresentations%2Fcamp1_2006%2Funionfind-handout.pdf&ei=UydjTKqaJIW8lQfZ0IjCDw&usg=AFQjCNGNSiYFJv4MADuMR4EgCkLln5Blmg). 49 50 Back to the problem: Every day we are allowed to build exactly 1 road, and close exactly 1 road. Thus, we can break the problem into two parts: 51 52 * How do we connect the parts of the graph that are disconnected? 53 * How do we remove roads in a way that does not disconnect parts of the graph? 54 55 Let *build* be the list all roads that need to be built, and let *close* be the list of nodes that need to be closed. We can show that in fact, these lists are of the same size. This is because the connected graph with *n* nodes is a tree if and only if it has *n* - 1 edges. Thus, if we remove more roads than than we build, then the graph is disconnected. Also, if we build more roads than we remove, then we have some unnecessary roads (the graph is no longer a tree). 56 57 Now consider the format of the input data: 58 *a*1, *b*1 59 *a*2, *b*2 60 ... 61 *a**n* - 1, *b**n* - 1 62 We can show that edge (*a**i*, *b**i*) is unnecessary if and only if the nodes *a**i*, *b**i* have already been connected by edges (*a*1, *b*1), (*a*2, *b*2), ..., (*a**i* - 1, *b**i* - 1). In other words, if the vertices *a**i*, *b**i* are in the same connected component *before* we, add (*a**i*, *b**i*) then we do not need to add (*a**i*, *b**i*). We can use union-find to help us solve this problem: 63 64 <code> 65 for( i from 1 to n-1 ) 66 { 67 if( find(*a**i*)=find(*b**i*) ) close.add(*a**i*, *b**i*); 68 else merge(*a**i*, *b**i*); 69 } 70 </code> 71 72 In other words, we treat each connected component as a set. Union find allows us to find the connected component for each node. If the two connected components are the same, then our new edge is unnecessary. If they are different, then we can merge them together (with union find). This allows us to find the edges that we can remove. 73 74 In order to find the edges that we need to add to the graph, we can also use union-find: whenever we find a component that is disconnected from component 1, then we just add an edge between them. 75 76 <code> 77 for( i from 2 to n ) 78 if( find(*v**i*)!=find(*v*1) ) 79 { 80 then merge(*v*1, *v**i*); 81 build.add(*v*1, *v**i*); 82 } 83 </code> 84 85 We just need to store the lists of roads that are unnecessary, and the roads that need to be built. 86 87 88 89 ### Problem E - Test 90 91 The way I solved this problem is with a [hash function](https://en.wikipedia.org/wiki/Hash_code). Hash functions can fail on certain cases, so in fact, my solution is not 'correct'. However, it passed all the test cases =P 92 93 Let the input strings be *s*0, *s*1, *s*2. We can build the shortest solution by permuting the strings and then trying to 'attach' them to each other. I.e., we need to find the longest overlapping segments at the end of string *a* and the beginning of string *b*. The obvious brute force solution won't run in time. However, we can use a hash function to help us calculate the result in *O*(*n*) time, where *n* is *min*(*len*(*a*), *len*(*b*)). The hash function that I used was the polynomial *hash*(*x*0, *x*1, ..., *x**n*) = *x*0 + *ax*1 + *a*2*x*2 + ... + *a**n**x**n*. This polynomial is a good hash function in this problem because it has the following useful property: 94 Given *hash*(*x**i*, ..., *x**j*), we can calculate the following values in *O*(1) time: 95 96 * *hash*(*x**i* - 1, *x**i*, ..., *x**j*) = *x**i* - 1 + *a* × *hash*(*x**i*, ..., *x**j*) 97 * *hash*(*x**i*, ..., *x**j*, *x**j* + 1) = *hash*(*x**i*, ..., *x**j*) + *a**j* + 1 - *i* × *x**j* + 1 98 99 In other words, if we know the hash for some subsequence, we can calculate the hash for the subsequence and the previous element, or the subsequence and the next element. Given two strings *a*, *b*, we can calculate the hash functions starting from the end of *a* and starting from the beginning of *b*. If they are equal for length *n*, then that means that (maybe) *a* and *b* overlap by *n* characters. 100 101 Thus, we can try every permutation of *s*0, *s*1, *s*2, and try appending the strings to each other. There is one last case: if *s**i* is a substring of *s**j* for some *i* ≠ *j*, then we can just ignore *s**i*. We can use hash functions to check that one string is contained within another one.