String Basics

Definition

Character Set

A character set Σ\Sigma is a set equipped with a total order, which means that any two distinct elements α\alpha and β\beta in Σ\Sigma can be compared: either α<β\alpha<\beta or β<α\beta<\alpha. The elements of the character set Σ\Sigma are called characters.

String

A string SS is a sequence formed by arranging nn characters in order. nn is called the length of SS, denoted by ∣S∣|S|.

If string indices start at 11, the ii-th character of SS is denoted by S[i]S[i];

if string indices start at 00, the ii-th character of SS is denoted by S[i−1]S[i-1].

Substring

The substring S[i..j],i≤jS[i..j],i≤j of string SS denotes the segment from ii to jj in SS, namely the string formed by arranging S[i],S[i+1],…,S[j]S[i],S[i+1],\ldots,S[j] in order.

Sometimes S[i..j]S[i..j], i>ji>j, is also used to denote the empty string.

Subsequence

A subsequence of string SS is a sequence formed by extracting some elements from SS without changing their relative positions, namely S[p1],S[p2],…,S[pk]S[p_1],S[p_2],\ldots,S[p_k], where 1≤p1<p2<⋯<pk≤∣S∣1\le p_1< p_2<\cdots< p_k\le|S|.

Suffix

A suffix is a special substring that starts at some position ii and ends at the end of the entire string. The suffix of string SS starting at ii is denoted by Suffix(S,i)\textit{Suffix(S,i)}, namely Suffix(S,i)=S[i..∣S∣−1]\textit{Suffix(S,i)}=S[i..|S|-1].

A proper suffix is a suffix of SS other than SS itself.

For example, all suffixes of the string abcabcd are {d, cd, bcd, abcd, cabcd, bcabcd, abcabcd}, and its proper suffixes are {d, cd, bcd, abcd, cabcd, bcabcd}.

Prefix

A prefix is a special substring that starts at the beginning of the string and ends at some position ii. The prefix of SS ending at ii is denoted by Prefix(S,i)\textit{Prefix(S,i)}, namely Prefix(S,i)=S[0..i]\textit{Prefix(S,i)}=S[0..i].

A proper prefix is a prefix of SS other than SS itself.

For example, all prefixes of the string abcabcd are {a, ab, abc, abca, abcab, abcabc, abcabcd}, and its proper prefixes are {a, ab, abc, abca, abcab, abcabc}.

Lexicographic Order

Compare strings by using the ii-th character as the ii-th keyword. An empty character is smaller than every character in the character set (that is, a<aaa< aa).

Palindrome

A palindrome is a string that is the same when written forwards and backwards, that is, an ss satisfying ∀1≤i≤∣s∣,s[i]=s[∣s∣+1−i]\forall 1\le i\le|s|, s[i]=s[|s|+1-i].

Hamming Distance

The Hamming distance is the distance between two strings of equal length; it is the number of positions at which the corresponding characters of the two strings differ.

It can be simply understood as the number of 11 bits obtained by XORing the two strings.

String Storage

  • Store strings in a char array, using the null character \0 to mark the end of the string (a C-style string).
  • Use the string class provided by the C++ standard library.
  • String constants can be represented by string literals (strings enclosed in double quotation marks).

This content comes from OI Wiki: String Basics. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.