Algorithm question 2

Source: Internet
Author: User
1. Next we will introduce an efficient data structure and apply it to a very small problem: given an input file, look for the longest repeated substring in it. For example, the longest duplicate string in "Ask not what your country can do for you, but what you can do for your country" is "can do for you ", "Your country" is the second position. How do I compile a program to solve this problem?

Our program can process at most N maxcharacters, which are stored in array C: # define maxn 5000000 char C [maxn], * a [maxn]; we will use a simple data structure "suffix array". Although this term was introduced in 1990, it was used at least in 1970. This structure is a pointer array a pointing to characters. When we read the input, initialize a first, so that each element points to the corresponding character in the input string: While (CH = getchar ())! = EOF) A [n] = & C [N] C [n ++] = ch c [N] = 0 the last element in array C is an empty character, it terminates all strings. Element A [0] points to the entire string, the next element points to the suffix of the array starting with the second character, and so on. After the input string "banana", the array indicates these suffixes: A [0]: banana a [1]: Anana A [2]: Nana a [3]: ana a [4]: na a [5]: A. Since the pointer in array a points to each Suffix in the string, array a is named "suffix array ". If a long string appears twice in array C, it has two different suffixes. Therefore, we find the same suffix for the sort array. The "banana" array is sorted into a [0]: AA [1]: Ana a [2]: ananaa [3]: banana a [4]: na a [5]: nana can then scan the adjacent element of the array to find the longest repeated string. In this example, we use the qsort function to sort the suffix array: qsort (A, N, sizeof (char *), pstrcmp) the comparison function pstrcmp adds an indirect layer to the library function strcmp. This array scan uses the comlen function to count the same number of characters in the two adjacent words: for I = [0, n) If comlen (A [I], a [I + 1])> maxlen = comlen (A [I], A [I + 1]) maxi = I printf ("%. * s/n ", maxlen, a [Maxi]); the printf statement uses" * "to accurately output the maxlen characters of a string. # Include <stdlib. h> # include <string. h> # include <stdio. h> int pstrcmp (char ** P, char ** q) {return strcmp (* P, * q);} int comlen (char * P, char * q) {int I = 0; while (* P & (* P ++ = * q ++) I ++; return I ;} # define M 1 # define maxn 5000000 char C [maxn], * a [maxn]; int main () {int I, CH, n = 0, Maxi, maxlen =-1; while (CH = getchar ())! = EOF) {A [n] = & C [N]; C [n ++] = CH;} C [N] = 0; qsort (A, N, sizeof (char *), pstrcmp); for (I = 0; I <n-m; I ++) if (comlen (A [I], A [I + M])> maxlen) {maxlen = comlen (A [I], a [I + M]); Maxi = I;} printf ("%. * s/n ", maxlen, a [Maxi]); Return 0 ;}

The complexity of this algorithm is O (n log n)

2. The input is a Number Matrix of n × n and has been read into the memory. Each row is added from left to right. Each column is added from top to bottom. Returns an O (n) Worst Case algorithm to determine whether the number X is in the matrix.

K is the number to be searched, I, X is the abscissa, J, Y is the ordinate.
1. If K> M [I] [J] => for all elements M [x] [Y], x> = I, Y> = J
2. If K = m [I] [J] finds
3. If K <m [I] [J] => K is greater than all elements M [x] [Y], x <= I, Y <= J

Therefore, you only need to construct a search method (it is easier to understand the image, but there is no way to map it here ):
1. First, search for J from I = 0, j = 0, and obtain the first J with a value greater than K on the rightmost end. It is recorded as Jmax here. At this point can be introduced: If K exists, then the K element m [x] [Y] must have x> = I, Y <= jMax-1, that is to say, it must be in (I, Jmax) bottom left.
2. Then start from I = 0, j = jMax-1:
3. If K> M [I] [J], then ++ I can go down because the elements of x <= I y <= J are smaller than K.
4. if K <m [I] [J], -- J. Therefore, at this time, the X> = I y> = J element is greater than K, so it can only go to the left, because the above numbers must be greater than or less than K.
5. Repeat Step 3 and 4 until no or no

Time complexity O (2n)
The program code is as follows:

Bool search (int * array, int L, int R, int num)
{
Int I = 0;
Int J = 0;
For (; j <r; ++ J)
{
If (array [I * r + J]> num)
Break;
Else if (array [I * r + J] = num)
Return true;
}

-- J;
While (1)
{
If (array [I * r + J]> num) // left
J --;
Else if (array [I * r + J] <num) // go down
I ++;
Else
Return true;
If (I> = L | j <0) // cannot be found
Return false;
}
Return false;
}
Called search (& M [0] [0], n, n, num)
For two-dimensional arrays:
(1) Neither dimension is given, and both dimensions are used as form parameters:
void func(double a[][], int dim1,int dim2);
(2) directly use the array name as a pointer to the pointer:
Void func (double ** A, int dim1, int dim2 );
This is all wrong. Because the parameter declaration a [] [] itself is invalid. Because, to determine the element location, the second dimension after the multi-dimensional array must be known. A correct solution is:
Void print (int * a, int dim1, int dim2)
{
For (INT I = 0; I <dim1; I ++ ){
For (Int J = 0; j <dim2; j ++)
Cout <A [I * dim2 + J] <'/T ';
Cout <'/N ';
}
}
The first parameter in the function call is the address of the first element.
Of course, you can use
Define two-dimensional dynamic arrays to avoid the above problems

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.