The depressing thing happened to me. Yesterday, I used live writer to write <data structure and algorithm-string (I)>. It was published successfully and I personally checked it. When I finish writing <data structure and algorithm --- string (medium)>, the release is successful. My blog only has the newly released data string (medium). The data string (on) is missing. I am so depressed that I cannot find the backup. It seems that the words of the ancients still make sense: "to do good deeds, you must first sharpen the tools ". You can only blame yourself for not using the live Writer Tool. Fortunately, all the knowledge is stored in your mind. Let's learn about the data structure of strings.
String Concept
Computers are generated to meet people's needs for numerical computing. In the early days, computers were not physically larger, but computed faster. They were no different from calculators. However, as people's needs continue to increase, computer numerical computing is no longer able to meet people's needs. Therefore, the computer began to introduce character computation, which had the concept of a string.
String: a finite sequence consisting of zero or multiple characters.
The number of characters in a string can be zero. In this case, the string is called a null string and the length is 0. Note that empty strings and space strings are different. An empty string consists of zero characters and has a length of 0. A space string consists of n space characters. The character length is N. See the following code:
Static void main (string [] ARGs) {string sentence1 = ""; // sentence1 is an empty string and the length is 0 string sentence2 = ""; // sentence2 is a space string, length: 3}
If the T string exists in the S string, the T string is called the S string. Correspondingly, the S string is called the main string of the T string. For example:
Static void main (string [] ARGs) {string sentence1 = "Believe in yourself"; // The sentence1 is the primary string of sentence2, because it contains the setence2 character string sentence2 = "yourself"; // sentence2 is a substring of sentence1, because sentence2 exists in sentence1}
String Storage Structure
String storage structure, similar to linear tables. Strings are also divided into two storage structures: sequential storage and linear storage.
Sequential storage: stores character sequences in strings with a set of sequential address storage units. We generally use arrays to define them. We are used to store the array subscript at zero position to the length of the string.
Chain storage: The chain storage of strings is similar to the chain storage of linear tables. However, due to the special nature of the string structure, each element in the structure is a character. If you store one character at each node according to the chain storage of linear tables, it will cause a great waste of space. A node can store one character or multiple characters.
Abstract string data type
We usually use string objects in project development. We can use a String object to solve many requirements. For example, we can use the index (S, T) method of the string class to determine whether the S string contains the T string. We can use the compare () method of the string class to check whether the password and user name entered by the user are correct. Let's take a look at the definition of abstract data types of strings:
The element in the string data string is composed of only one character. The adjacent element has a precursor and a Successor Relationship. Operation strassign (T, * chars): generate a string T whose value is equal to the Character String constant chars. Strcopy (t, s): the string s exists and is copied from the string S. Clearstring (s): The string s exists and the string is cleared. Stringempty (s): If the string S is null, true is returned; otherwise, false is returned. Strlength (s): returns the number of elements in string s, that is, the length of the string. Strcompare (S, T): If S> T, return value> 0. If S = T, return value = 0. If S <t, return value <0. Concat (T, S1, S2): use t to return a new string connected by S1 and S2. Substring (sub, S, POs, Len): the string s exists, 1 <= POS <= strlength (s), and 0 <= Len <= strlength (s) -pos + 1: return the index (S, T, POS) of the long string after the POs position in S string using Sub: returns the position of the string t for the first time after the POs position in S string, if no value exists, 0 is returned. Repalce (S, T, V): the string S, T, and V exist. T is a non-empty string. Replace all non-overlapping strings equal to T in the primary string s with v. Strinsert (S, POs, T) // 1 <= POS <= strlength (s) + 1 insert the string t strdelete (S, POs, Len) before the POs position in the main string) // 1 <= POS <= strlength (s), 0 <Len <= strlength (S)-pos + 1 Delete the string endadt whose POS position is Len
These are the abstract data types of strings, and some operations can be used together. In the data structure and algorithm-string (below), I will explain in detail the implementation algorithms of the above operations. Algorithm + Data Structure = programming. Qualified programmers must learn more algorithms and master their principles. This is easy to learn. I explained the KMP Matching Algorithm in the data structure pre-algorithm-string (medium). You can take a look.
Data Structure and algorithm-string (I)