修改:原代码使用了非编译期常量声明数组

现将int next[needle.size()];替换为vector<int> next(needle.size());
This commit is contained in:
nuo
2024-01-07 05:07:06 +08:00
parent 6b0cc1d5ff
commit e4db0542d4

View File

@ -425,8 +425,8 @@ public:
if (needle.size() == 0) {
return 0;
}
int next[needle.size()];
getNext(next, needle);
vector<int> next(needle.size());
getNext(&next[0], needle);
int j = -1; // // 因为next数组里记录的起始位置为-1
for (int i = 0; i < haystack.size(); i++) { // 注意i就从0开始
while(j >= 0 && haystack[i] != needle[j + 1]) { // 不匹配
@ -524,8 +524,8 @@ public:
if (needle.size() == 0) {
return 0;
}
int next[needle.size()];
getNext(next, needle);
vector<int> next(needle.size());
getNext(&next[0], needle);
int j = 0;
for (int i = 0; i < haystack.size(); i++) {
while(j > 0 && haystack[i] != needle[j]) {
@ -1428,4 +1428,3 @@ public int[] GetNext(string needle)
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>