一、字元串查找子串函數
在編寫字元串處理程序時,字元串查找子串函數是非常實用的。在C++中,常用的字元串查找子串函數為std::string::find()。這個函數可以用來查找字元串中的子串。例如:
std::string str = "hello world";
int index = str.find("world");
這個例子中,我們定義了一個字元串str,並使用std::string::find()函數查找其中的子串”world”,得到了這個子串在字元串中的位置。
在Python中,可以使用字元串自帶的方法find()來查找子串,例如:
str = "hello world"
index = str.find("world")
二、字元串查找子串的個數
有時候我們需要統計字元串中子串出現的次數,C++和Python都提供了方法用來實現這個功能。
在C++中,我們可以使用std::string::find()函數和循環來查找字元串中子串出現的次數。例如:
std::string str = "hello world";
std::string substr = "l";
int count = 0;
size_t pos = 0;
while ((pos = str.find(substr, pos)) != std::string::npos) {
++count;
pos += substr.length();
}
這個代碼段中,我們定義了一個變數count來保存查找到的子串的個數,然後使用循環和std::string::find()函數來查找每個子串出現的位置,並將count加一。
在Python中,可以使用字元串自帶的方法count()來查找子串的個數。例如:
str = "hello world" substr = "l" count = str.count(substr)
三、字元串查找子串可以使用index
在Python中,字元串自帶的方法index()也可以用來查找子串。與find()函數不同的是,當子串不存在時,index()函數會拋出一個異常,而find()函數會返回std::string::npos。例如:
str = "hello world"
index = str.index("world")
四、字元串查找子串出現次數
除了統計子串出現的次數外,有時候我們還需要知道每個子串出現的位置。在C++中,可以使用std::string::find()函數和std::vector來實現這個功能。例如:
std::string str = "hello world";
std::string substr = "l";
size_t pos = 0;
std::vector indices;
while ((pos = str.find(substr, pos)) != std::string::npos) {
indices.push_back(pos);
pos += substr.length();
}
這個代碼段中,我們定義了一個std::vector來保存查找到的子串的位置,然後使用循環和std::string::find()函數來查找每個子串出現的位置,並將位置保存到vector中。
在Python中,可以使用字元串自帶的方法finditer()來查找子串出現的位置。這個方法返回一個生成器,可以使用for循環來遍歷每個子串的位置。例如:
str = "hello world" substr = "l" indices = [m.start() for m in re.finditer(substr, str)]
五、字元串查找子串最快演算法
在C++中,可以使用KMP演算法來實現字元串查找子串功能。KMP演算法是一種線性時間複雜度的演算法,比std::string::find()函數更快,特別是對於長字元串和重複出現的子串。KMP演算法的基本思想是在匹配的過程中,當發現不匹配時,不需要回溯,而是通過已經匹配的信息,直接跳過一部分文本。
以下是KMP演算法的實現代碼:
void computeLPSArray(std::string substr, int* lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < substr.length()) {
if (substr[i] == substr[len]) {
++len;
lps[i] = len;
++i;
} else {
if (len != 0) {
len = lps[len-1];
} else {
lps[i] = 0;
++i;
}
}
}
}
int KMP(std::string str, std::string substr) {
int n = str.length();
int m = substr.length();
int lps[m];
computeLPSArray(substr, lps);
int i = 0;
int j = 0;
while (i < n) {
if (substr[j] == str[i]) {
++i;
++j;
}
if (j == m) {
return i-j;
} else if (i < n && substr[j] != str[i]) {
if (j != 0) {
j = lps[j-1];
} else {
++i;
}
}
}
return -1;
}
在Python中,也可以使用KMP演算法來實現字元串查找子串功能。例如:
def computeLPSArray(substr):
len = 0
lps = [0] * len(substr)
i = 1
while i < len(substr):
if substr[i] == substr[len]:
len += 1
lps[i] = len
i += 1
else:
if len != 0:
len = lps[len-1]
else:
lps[i] = 0
i += 1
return lps
def KMP(str, substr):
n = len(str)
m = len(substr)
lps = computeLPSArray(substr)
i = 0
j = 0
while i < n:
if substr[j] == str[i]:
i += 1
j += 1
if j == m:
return i-j
elif i < n and substr[j] != str[i]:
if j != 0:
j = lps[j-1]
else:
i += 1
return -1
六、字元串查找子串演算法
除了KMP演算法,還有其他演算法可以用來實現字元串查找子串功能。例如,Brute-Force演算法就是一種樸素的演算法,在每個位置都嘗試查找子串,並比較字母。這種演算法的時間複雜度為O(mn),其中m是子串長度,n是字元串長度,效率較低。
以下是Brute-Force演算法的實現代碼:
int BruteForce(std::string str, std::string substr) {
int n = str.length();
int m = substr.length();
for (int i=0; i<=n-m; ++i) {
int j;
for (j=0; j<m; ++j) {
if (str[i+j] != substr[j]) {
break;
}
}
if (j == m) {
return i;
}
}
return -1;
}
七、字元串查找子串C代碼
以下是使用std::string::find()函數實現字元串查找子串的C代碼:
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "hello world";
char substr[10] = "world";
char* p = strstr(str, substr);
if (p) {
printf("%s found at position %d.\n", substr, p-str);
} else {
printf("%s not found.\n", substr);
}
return 0;
}
八、字元串查找子串Python
以下是使用字元串自帶方法find()實現字元串查找子串的Python代碼:
str = "hello world"
substr = "world"
index = str.find(substr)
if index != -1:
print(substr, "found at position", index)
else:
print(substr, "not found.")
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/257788.html
微信掃一掃
支付寶掃一掃