Data structure, data structure and Algorithm

Source: Internet
Author: User

Data structure, data structure and Algorithm
What is the concept of a string?

String: a finite sequence composed of zero or multiple characters. As: s = 'a1a2... An '(n ≥ 0)

S is the string name, 'a1a2... An 'is the string value, and n is the length of the string.

Position of the substring in the primary string: expressed by the position of the first character in the primary string.

Equal strings: if and only when the values of the two strings are equal (the two strings are of the same length and each character is the same

String Data Type
ADT String {D = {ai | ai ε CharacterSet, I = 1, 2 ,..., n, n ≥ 0} R = {<ai-1, ai> | ai-1, ai, D, I = 2 ,..., n} basic operations :......}

Where D is the data object R is the Data Relationship
A string is a finite length Character Sequence enclosed by a pair of single quotes, such as "a string"

String operations
  • String assignment: StrAssign (& S, chars)
  • Evaluate the string length: StrLength (S)
  • String determination: StrCompare (S, T)
  • Connection: Concat (& T, S1, S2)
  • SubString: SubString (& sub, S, pos, len)
  • Substring positioning: Index (S, T, pos)
  • Replace: Replace (& S, T, V)
  • Insert substring: StrInsert (& S, pos, T)
  • Delete substrings: StrDelete (& S, pos, len)

    Algorithm for locating Function Index

    Index (S, T, pos) is used to determine the position of the T string in the S string.
    Analysis: it can be achieved through operations such as string determination, String Length and substring determination.

    Algorithm:
int Index (String S, String T, int pos) {    if (pos > 0) {           n = StrLength(S);            m = StrLength(T);            i = pos;        while ( i <= n-m+1) {           SubString (sub, S, i, m);           if(StrCompare(sub,T) != 0)              ++i;           else return i;        }     }      return 0;         } 

If return 0, it means that there is no substring equal to T in S.
T is a non-empty string. If there is a substring equal to T after the second pos character in the main string S, the position of the first substring in S is returned; otherwise, 0 is returned.

String operation example (*)

1. SubString (& sub, S, pos, len)

SubString (sub, "commander", 4, 3)
Obtain sub =?
'Man'
Take 3 elements from the first element of "commander"

SubString (sub, commander, 1, 9)
Obtain sub =?
'Commander'
Obtain 9 elements starting from the 1st elements of "commander"

SubString (sub, "commander", 9, 1)
Obtain sub =?
'R'
Obtain 1 element from the first element of "commander ".
SubString (sub, commander, 4, 7)
Obtain sub =?
'Mander'
SubString (sub, "beijing, 7, 2)
Obtain sub =?
'G'
Obtain 1 element from the first element of "commander ".

SubString (sub, "student", 5, 0)
Obtain sub =?
''

Summary

The logical structure of a string is very similar to that of a linear table. The difference is that the data object constraints of a string are character sets.
The basic operations of strings are very different from those of linear tables:
1. In the basic operations of a linear table, most of them use a single element as the operation object.
2. In the basic operations of a string, the "whole string" is usually used as the operation object.

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.