Arrays and strings

Source: Internet
Author: User

Arrays are a common data structure, and high-level language headers provide basic operations to support arrays, and arrays are an important component of other data structures.

An array is a finite sequence of memory cells that occupy a contiguous number of addresses, consisting of n identical elements.

Any element of an array can be represented by the position of the array.

Arrays differ from linear tables: arrays conform to the definition of a linear structure.

But the difference is that the array requires a contiguous address space, the elements of the linear table are indivisible, the array can be a two-dimensional array, and the main operation of the array is to store and read the data.

The implementation mechanism of the array:

Typically, bytes are internal count units. For a one-dimensional array with n data elements, A0 is an array element with subscript 0, loc (a0) 's Memory cell address, and K is the number of bytes required for each array element, the memory unit address of the AI for any of the data elements in the array is Loc (AI), which can be calculated by the following company:

LOC (AI) = loc (a0) + i*k; (0<=i<=n)

For a two-dimensional array m row n column

LOC (AI) = loc (A00) + (i*n+j) *;(0<=i<=m) (0<=j<=n);

String:

string is a linear structure, and the difference between the linear table is: The operation of the string is characterized by the operation of several data elements, that is, a substring. Strings can be stored using sequential structures and chained structures. The sequential structure of the string is more efficient in storing space and time. Pattern matching is a very important operation of a string. The Brute-force and KMP algorithms are two of the most commonly used string pattern matching algorithms.

The basic concept of a string: A string is a finite sequence of n>=0 characters. A subsequence of any contiguous character in a string is called a substring of that string.

strings and characters are not a concept: strings are a set of variable-length sequences of characters. The character is just a character.

Abstract data types for strings:

The string can be made up of S0, ... The combination of SN characters.

The collection of actions:

Initializes the string;

Assignment string

String length

Compare characters

Insert character

Delete character

Take substring

Find a child string

Replace substring

The C language stores strings in a character array. The length of the string is variable, and the C language solves the indefinite length by automatically adding a '/s ' as the end flag of the string at the end of the string.

The storage structure of the string:

The storage structure of string has two kinds of sequential storage structure and chain storage structure.

Sequential storage structure for strings:

As with the storage structure of a linear table, you can store string values in an array of character types.

In the sequential storage structure of a string, because of the variable length of the string, a method is used to identify the length of the string, one is fixed set string length, and the other is the end flag of the string using the.

Chained storage structure of the string:

Chain chain structure is to put the string value is not stored in the form of a number of nodes in the list of data fields. String chain structure is divided into two kinds of single-Fu and block-chain.

The word linked is a node that stores one character.

A block chain is a node that stores several characters.

The basic implementation of the string is like a linear table implementation method.

Use PHP instead:

<?php

/**
*
* Implementation of the String
*/
Header (' Content-type:text/html;charset=utf-8 ');

Class Char {
Public $size = 0; Number of elements rather than array size
Public $arr = Array ();
Public $maxsize = 10; Array size


Public function __construct ($arr = Array ()) {
$this->size = count ($arr);
$this->arr = $arr;
}

Public function sizes () {return size;}
/**
* @param int start position
* @param string of strings inserted
* @return BOOL
*/
Public function Add ($start, $str) {
$len = strlen ($STR);
if (($start + $len) > $this->maxsize) {
Return false;//Insert data beyond character size
} else {
for ($i = $this->size-1; $i >= $len; $i-) {
$this->arr[$i + $len] = $this->arr[$i];
}
for ($i = 0; $i < $len; $i + +) {
$this->arr[$start + $i] = $str {$i};
}
$this->size = $this->size + $len;
}
}
/** 0,1,2,3,4,5,6
* Delete String
* @param int start position
* Number of @param int deleted
*/
Public Function Delete ($start, $len) {
if (($start + $len) > $this->maxsize) {
return false;
} else {
for ($i = $start + $len; $i <= $this->size-1; $i + +) {
$this->arr[$i-$len] = $this->arr[$i];
}
$this->size = $this->size-$len;
for ($i = $this->size-$len; $i <= $this->size-1; $i + +) {
unset ($this->arr[$si]);
}
}
}
/** 0,1,2,3,4,5,6
* Take string
* @param int start position
* Number of @param int deleted
*/
Public substr ($start, $len) {
for ($i =0; $i < $len; $i + +) {
$str. = $this->arr[$i + $start];
}
return $str;

}

Public Function Getstr () {echo implode (', $this->arr);}
Pattern matching of/*************** strings ***************/


}

$char = new char ();
$char->add (0, ' 1 ');
$char->getstr ();

Pattern Matching for strings:

Brute-force algorithm

BF algorithm is a common pattern matching algorithm, the idea of BF algorithm is to match the first character of the target string s with the first character of the pattern string p, if equal, continue to compare the second character of S and the second character of P, if not equal, compare the second character of S with the first character of P, and then compare to the next one. Until the final matching result is reached.

     function BF ($str, $t) {
        for ($i =0; $i <strlen ($ STR), $i + +) {
            for ($j =0; $j <strlen ($t); $j + +) {
                if ($str {$i}== $t {$j}) {$i + +;} else {echo ' & lt; '. $i. '-$j. ' > '; $i = $i-$j; break;}
            }
            if ($j ==strlen ($t)) return ' true ';
        }
        return ' flase ';
    }

function BF2 ($STR, $t) {
$i = $j = 0;
while ($i <strlen ($str)) {
$j = 0;
while ($str {$i} = = $t {$j} && $j <strlen ($t)) {
$i + +;
$j + +;
}
if ($j ==strlen ($t)) return ' true ';
}
Return ' flase ';
}

KMP algorithm is the optimization of BF algorithm

function Indexnext ($STR) {
$k = 0;
$j = 1;
$nexts [0] =-1;
$nexts [1] = 0;
while ($j <strlen ($str)) {
if ($str {$j} = = $str {$k}) {
$nexts [$j +1] = $k +1;
$j + +;
$k + +;
} else if ($k ==0) {
$nexts [$j +1] = 0;
$j + +;
} else {
$k = $nexts [$k];

}
}
return $nexts;
}

function KMP ($STR, $t) {
$nexts = Indexnext ($t);
Print_r ($nexts);
$i = 0; $j = 0;
$k = 0;
while ($i <strlen ($str) && $j < strlen ($t)) {
$k + +;
if ($j = =-1 | | $str {$i} = = $t {$j}) {
$i + +; $j + +;
}else {
echo $i. '-' $j. ' * ';
$j = $nexts [$j];
}

if ($j ==strlen ($t)) {echo $k; return ' true ';}
}
Return ' false ';

}

Ask the vast number of cattle people, so the internship KMP algorithm right?

Arrays and strings

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.