Array & String
Outline
1. String match for entry question
2. Application of HashTable in Array
3. String in C/A + +
4. Example Analysis
Part 1 Entry Questions
In source (parent string), look for whether the target (substring) appears.
/* Returns The position of the first occurrence of string target instring source, or-1 If Target is not part of source. */int strStr (string source, string target) {//...}
String match
Two more easy-to-implement string comparison algorithms:
Assume that a substring of length m is matched in a string of length n. (Http://en.wikipedia.org/wiki/String_searching_algorithm).
1. brute-force algorithm: Sequential traversal of the parent string, each character as a matching starting character, determine whether to match the substring. Time complexity O (m*n)
Char* STRSTR (Const Char*STR,Const Char*target) { if(!*target)returnstr; Char*P1 = (Char*) str; while(*p1) { Char*p1begin = P1, *p2 = (Char*) target; while(*p1 && *p2 && *p1 = = *p2) {P1++; P2++; } if(!*P2)returnP1begin; P1= P1begin +1; } returnNULL;}
2.rabin-karp algorithm: Map each gamete string to a hash value. For example, consider a substring as a number of digits, compare its value to the hash value of the same length substring in the string, and, if it is the same, then carefully confirm that the string is exactly the same as the character. In order to calculate the hash value of the parent string, the increment calculation method is used: the hash value of the highest bit is deducted, and the hash value of the lowest bit is added. Thus can be done on average O (m+n).
#include <string>#include<iostream>#include<functional>using namespacestd;size_t Rabin_karp_strstr (string&source,string&target) {Hash<string>Strhash; size_t Targethash=Strhash (target); for(size_t i =0; I < Source.size ()-target.size () +1; i++) { stringSubsource =source.substr (i, target.size ()); if(Strhash (subsource) = =Targethash) { if(Subsource = =target)returni; } } return 0;}intMain () {stringS1 ="ABCD"; stringS2 ="CD"; cout<<rabin_karp_strstr (S1, S2); return 0;}
Part 2 Array
int // defines an integer array of length arraysize on a stack int New int // defines an integer array of length arraysize on the heap Delete // you need to free up memory when you are finished using:
Note that in the old compiler, you cannot define an array of length uncertainties on the stack, which can only be defined as follows:
int array[10];
The new compiler does not have the upper limit. However, you cannot initialize an array if the array is of variable length:
int Array[arraysize] = {0};//initialize array of indefinite length to zero, compile error.
Part 3 Toolbox: Stack Vs. Heap
Stack primarily refers to the memory space that is automatically managed by the operating system. When entering a function, the operating system allocates storage space for local variables in the function. In fact, the system allocates a block of memory superimposed on the current stack and uses the pointer to point to the address of the previous memory block.
The local variables of the function are stored on the current block of memory. When the function returns, the system "pops up" the memory block, and returns to the previous block of memory based on the pointer. So, the stack always works in last-in, first-out (LIFO) mode
Heap is the space used to store dynamically allocated variables. For heap, there is no last-in-first rule like stack, and programmers can choose to allocate or reclaim memory at any time. This means that the programmer needs to use the command to reclaim the memory, otherwise it will create a memory leak (leak).
In C + +, programmers need to call Free/delete to release dynamically allocated memory. In Java,objective-c (with Automatic Reference count), the language itself introduces garbage collection and counting rules to help users decide when to automatically free up memory.
Part 42-dimensional array
//Create on stack:intArray[m][n];//Pass to child function:voidFuncintArr[m][n]) { /*m can be omitted, but n must exist so that the compiler determines the spacing of the moved memory*/}//Create on Heap:int**array =New int*[M];//or (int**) malloc (M * sizeof (int*)); for(inti =0; i < M; i++) array [i]=New int[N];//or (int*) malloc (N * sizeof (int));//Pass to child function:voidFuncint**arr,intMintN) {}//you need to free up memory when you are finished using: for(inti =0; i < M; i++) Delete[] array[i];Delete[] array;
Part 5 Toolbox: Vector
Vectors can access elements directly using the operator []. Please refer to http://www.cplusplus.com/reference/vector/vector/
Size_type size ()Const;//Returns the number of elements in the vector.voidPush_back (Constvalue_type&val);voidPop_back (); iterator Erase (iterator first, iterator last);//removes from the vector either a singleelement (position) or a range of elements ([First,last)]. for(vector<int>::iterator it = V.begin (); It! =v.end ();) { if(condition) {It=V.erase (IT); } Else { ++it; }}
Part 6 Hash Table
Hash table is almost the most important data structure, mainly used to find based on "key", the basic element of storage is key-value pair. Logically, an array can be a special case of a Hash table: key is a non-negative integer.
Operations
- Insert-o (1)
- Delete-o (1)
- Find-o (1)
Hash collision-Open Hashing (re-hash) vs Closed Hashing (chain address method)
Part 7 C + + standard library
Provides a map container, can be inserted, delete, find Key-value pair, the bottom of the way to balance binary search tree implementation, according to key to sort.
In c++11, the standard library added unordered_map, more in line with the traditional definition of hash table, average time to find O (1)
Part 8 String
In C, a string refers to a char array that ends with '/'. Functions about strings usually need to pass in a character-type pointer.
In C + +, string is a class and can be manipulated by invoking a class function to determine the length of a string, a substring, and so on.
Part 9 Toolbox: string common functions in the C language
Char*strcpy (Char*destination,Const Char*source);//Copy source string to destination stringChar*strcat (Char*destination,Const Char*source);//appends a copy of the source string to the destination string.intstrcmp (Const Char*STR1,Const Char*str2);Char*STRSTR (Char*STR1,Const Char*STR2);//Returns A pointer to the first occurrence of str2 in str1, or a NULL pointer if str2 are not part of STR1.size_t strlen (Const Char*STR);//Returns The length of the C string str.DoubleAtof (Const Char*STR);//Convert char string to a doubleintAtoi (Const Char*STR);//Convert char string to an int
Part 10 Toolbox: String class Common functions in C + +
The String class overloads the +, <,,, =, = = operators, so copy, compare, determine whether equal, additional substrings, etc. can be directly implemented with the operator. Please refer to (http://www.cplusplus.com/reference/string/string/)
size_t Find (Const string& str, size_t pos =0)Const;//searches the string for the first occurrence of the STR, returns indexstringsubstr (size_t pos =0, size_t len = NPOs)Const;//Returns A newly constructed string object with it value initialized to a copy of a substring starting at Pos with Le Ngth Len.string&erase (size_t pos =0, size_t len = NPOs);//Erase characters from Pos with length lensize_t length ();//Returns The length of the string, in terms of bytes
Pattern recognition
When encountering certain topics that need to count the occurrences of an element in an element set, it should be intuitive to use Hash Table, that is, using Std::unordered_map or Std::map:key is an element, and value is the number of occurrences. In particular, there are some topics that only need to determine whether an element is present or not (equivalent to judging if value is 0 or 1), and you can use Bitvector, or bitset, to use a bit to indicate if the current subscript has a value.
Algorithm principles and practices (arrays and strings)