Classical algorithm face questions and answers

Source: Internet
Author: User
Tags abs numeric value

Classical algorithm face questions and answers
1. Hand hands coincide several times on the surface there are 60 of small lattice, each Georg represents one minute, the hand hour walks 1/12 small lattice, the minute hand walks 1 compartments every minute, from the first coincidence to the second coincidence the minute hand is more than the hand hour to walk a circle namely 60 small lattice, therefore 60/(1-1/12) =720/11 every 720/ 11 points to coincide once (instead of overlapping once per hour)
1440 has 22 720/11, if said to count 0 points and 24 points, it is also coincident 23, but I think 0 points should be counted to the previous day 24 points, so the cycle of each time to overlap 22 times AH
2. Find the longest repeating substring of the string, the output length is a 256-cell array, each cell represents a character, the array holds the last occurrence of that character, reads the string in turn, maintains the value of the array, and if it encounters a conflict, returns the position saved in the conflict character, and continues to step two. You can also use HashMap to save the position of characters and characters that appear
3. It is said that there is a text file, about 10,000 lines, one word per line, and that the first 10 words that appear most frequently are required to be counted. Use the hash first to count the number of occurrences of each word, and then find the first 10 words that appear most frequently by finding the number of the first k in the number of N.
4. Title 3, but the train files are particularly large, there is no way to read into memory. 1 The direct sort, write the file, while writing the string and its occurrence times. 2) can be hashed, for example, the string is divided into multiple regions based on the first character of the string, the string of each region is written into a file, and then a hash + heap is used to count the top 10 highest-frequency strings in each area, and the first 10 highest-frequency strings in all strings are finally calculated.
5. There is an integer n, which decomposes the sum of n into several integers, and asks how the decomposition can make the product of these numbers the largest and output the product m. For example: n=12 (1) is decomposed into 1+1+1+...+1,12 1, m=1*1*1......*1=1 (2) is decomposed to 2+2+...+2,6 2, m=64 (3) is decomposed to 3+3+3+3,4 3, m=81 (4) is greater than or equal to 4 o'clock decomposition only to 2 and 3, and 2 up to two F (n) = 3*f (n-3) n>4 f (4) = 2*2 F (3) = 3 F (2) = 2 decompose to 4+4+4,3 4, m=64
6. To find more than half of the number of occurrences in the array n the array is divided into [N/2] groups, at least one group contains duplicate numbers, because if there are no duplicates, the maximum number of occurrences equals half. The algorithm is as follows: k<-n; While k>3 do divides the array into [K/2] groups;     For the i=1 to [K/2] the Do if group is the same as the number of 2, then any number left; Else 2 numbers are thrown away at the same time;       k<-the remaining number if k=3 then to be compared with 2 numbers; If two numbers are different, then 2 numbers are thrown out else to take one number if k=2 or 1 then
7. A file contains up to n positive integers, and each number is less than seven n,n <=10. The number of duplicates does not occur. Requires that the number in a file be sorted, the available memory is 1M, and disk free space is sufficient. Do not put any problems to a very complex algorithm on the most direct and simple to solve the problem is the quality of engineers should have, the topic is very measured: N number, are less than N, 22 different, 1m=10^6byte=10^7bit memory, n <10^7 ideas: Think of 1 m memory as a bit array of length 10^7, each bit is initialized to 0 from the beginning of the number of N, if you touch I, put the bit array of the first position to 1,
1 m memory a bit less, (1M = 8M bits), can represent 8M Integer, now N <=10 seven times, you can read 2 times file, you can complete the sort. The first row n <8m to count, 2nd time row 8M <n <10 seven Times Square number.

8. There are 1 billion random numbers, how to find out the first 1000 of them as quickly as you can. 1 Build a 1000 number of heaps, the complexity of N (log1000) =10n 2 1. To identify the existence of an integer with each bit, such a byte can identify the existence of 8 integers, for all 32-bit integers, it takes 512Mb, So open up a 512Mb character array A, initial all 0 2. Reads each number n sequentially, sets a[n>>3] to a[n>>3]| (1<<n%8), which is equivalent to setting the corresponding bit of each number to 1 3. In a, the number of 1000 values from the large to the small is 1, which is the largest 1000 number. This reads the file only 1 times, and it should be the fastest method without considering the memory overhead.
9. A tree node 1, 2, 3, ..., N. How to achieve: first O (n) preprocessing, and then allow two nodes, O (1) to determine their parent-child relationship Dfs once, recording the start of each node access time Si and end access time Ei for two nodes i,j, if the interval [Si,ei] contains [Sj,ej], then I is the ancestor of J. Huffman coding for each node is also OK, but only for the general two-fork tree, and the actual problem is not necessarily binary, so the coding has limitations
10. Given a binary tree, find the nearest common ancestor node of the N (n>=2) node. Each node has only the left and right children pointer and no parent pointer. After the subsequent recursion to each node scoring, each node's score = Left score + right fraction +k, if a child is a given node +1 the deepest score n node is the request, the details should not be recursive end can get this node
11. How to print the following spiral queue: 21 22 .... 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14-13
#include <stdio.h> #define MAX (A,B) ((a) < (b)? ( b):(a) #define ABS (a) ((a) >0? (     A):-(a) int foo (int x, int y) {int t = MAX (ABS (x), ABS (y)); int u = t + t; int v = u-1; v = v * v + u; if (x = = t) V + + U + t-y; else if (y = = t) v + = 3 * U + x-t; else if (y = = t) v + = T-x; Else V + = y-t; return v;     int main () {int x, y; for (y=-2;y<=2;y++) {for (x=-2;x<=2;x++) printf ("%5d", foo (x, y)); printf ("\ n"); return 0; The No. 0 layer is defined as the middle of that 1, the 1th layer is 2 to 9, the 2nd layer is 10 to 25, ... Seems to have seen something. Note that 1, 9, 、...... is not the square number. And it's a continuous odd number (1, 3, 5 、...... ) is the square number. These numbers are also related to the number of layers, the calculation can be known in the T-layer a total of (2t-1) ^2 number, so that the T layer from [(2t-1) ^2] + 1 start to the outer spiral. Given the coordinates (X,Y), how to know where the point is at the first level. So easy, number of layers T = max (|x|,|y|).
Know the number of layers, the next good to do more, then we know that the point of the request must be in the T-layer this circle, down the number is just. Note that the spiral queue numeric growth direction and axis positive direction are not necessarily the same. We can divide into four kinds of situation--up, down, left, right--or--east, south, west, north, respectively, on the four side of the analysis.
East | right: x = = T, queue growth direction is consistent with Y axis, positive orient (y = 0) value is (2t-1) ^2 + t, so v = (2t-1) ^2 + t + Y
South | next: y = = t, queue growth direction and x axis opposite, positive south (x = 0) value (2t-1) ^2 + 3t, so v = (2t-1) ^2 + 3t-x
West | left: x = = T, queue growth direction and Y axis opposite, positive West (y = 0) numeric value (2t-1) ^2 + 5t, so v = (2t-1) ^2 + 5t-y
North | on: y = =-T, queue growth direction and X axis are the same, the northern direction (x = 0) value is (2t-1) ^2 + 7t, so v = (2t-1) ^2 + 7t + x
12. An integer that knows the number of digits and how to determine whether it can be divisible by 3, it is not possible to use division and modulo operations first 3x=2^n+1 only when n is odd because 2^n = 3x + ( -1) ^n; So the problem is converted to find the last bit a of 1, look forward to a 1 (b) and this bit The distance, if an even distance is not divisible, if it is odd, remove the bit after B to continue to judge
Seq=[a,b,..., z,aa,ab,..., az,ba,bb...,bz,... za,zb,..., zz,aaa ...], ask [a-z]+ (a string of arbitrary characters from A to Z) s in the position of SEQ, which is the rank The essence is 26 system.
As we all know, to see whether a number is divisible by 2 is only necessary to see whether it can be divisible by 2. But did you ever wonder why. This is because 10 can be divisible by 2, so a number of 10a+b can be divisible by 2 if and only if B can be divisible by 2. As we all know, to see if a number is divisible by 3 only depends on whether the sum of the numbers is divisible by 3. Why is that? The answer is more or less similar: because 10^n-1 can always be divisible by 3. 2345 can be written as 2* (999+1) + 3* (99+1) + 4* (9+1) + 5, the expansion is 2*999+3*99+4*9 + 2+3+4+5. The previous item with the number 9 must be able to be 3 whole, so to see whether 2345 can be divisible by 3 only need to see if 2+3+4+5 can be 3 whole except. Of course, this technique can only be used in the 10 system, but similar conclusions can be extended to arbitrary systems.      Note that 36 is the integer multiple of 4, while zzz ... ZZ divided by 7 always gets 555 ... 55. That is, to determine whether a 36 binary is divisible by 4 only needs to look at its single-digit, and a 36-digit number can be divisible by 7 and only when the sum of the numbers is divisible by 7. If a number is divisible by 4 and 7, the number must be divisible by 28. The question then translates to how many consecutive sentences satisfy the numbers and multiples of 7, while the last number is multiples of 4. In this way, we get an O (n) algorithm: Use p[i] to indicate how many of the previous sentences divided by 7 are I, scan the entire story and keep updating the P array. When the last word of a sentence is divisible by 4, assuming the prefix at the end of this sentence and dividing by 7 x, the value of the p[x at this time is added to the final output (the number of the two prefixes is the same as divided by 7 remainder, then the longer prefix will be divisible by 7).      The above algorithm is my intention to this problem, but after the game I saw a variety of other novel algorithms. For example, it is noted that 36^n MoD 28 is always equal to 8, and a similar linear algorithm can be constructed using this property. Some people use dynamic programming (or recursion) to solve the problem perfectly. We use f[i,j] to denote the number of text fragments that end with sentence I, divided by 28 digits J; We need to scan each of the different J, and add F[i-1,j to the corresponding F[i,j ' in the next sentence. Finally, the sum of all f[i,0] is output. This dynamic programming can be a scrolling array, so its space is as constant as the previous algorithm.      If you have no idea what I'm talking about, you can look at all the articles that are related to the carry system. In addition, I have had a very similar problem before (VOJ1090), you can compare to seeA look.

There is an integer n, which writes a function f (n), and returns the number of "1" that appears between 0 and N. For example f (13) = 6, now f (1) = 1, ask which n can satisfy F (n) =n.
For example: f (13) = 6, because the number of 1,2,3,4,5,6,7,8,9,10,11,12,13 1 is exactly 6.
public class Test {
public int n = 2;
public int count = 0;
public void bigestnumber (int num) {
for (int i = 1; I <= num; i++) {int m = 0;
int j = i; while (J > 0) {m = j% 10;
if (m = = 1) count++; if (J > 0) j = J/10;
}
}
System.out.println ("F" ("+ num +") = "+ count");

}

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.