c++ - 查找子字符串出现的次数

标签 c++ string performance find find-occurrences

我有一个小问题。我正在解决一项编程任务,但遇到了问题。这是一个简单的,但时间限制使它变得有点困难。

Find number of occurrences of substring. You will be given M - length of substring; substring to find, N - length of base string; base string.
M <= 100 000
N<= 200 000

Input

10
budsvabbud
79
uaahskuskamikrofonubudsvabbudnebudlabutkspkspkspmusimriesitbudsvabbudsvabbudnel

Output
3

我尝试使用内置函数查找,但速度不够快:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    int n;
    int occurrences = 0;
    string::size_type start = 0;
    string base_string, to_find;
    cin >> n >> to_find >> n >> base_string;
    while ((start = base_string.find(to_find, start)) != string::npos) {
        ++occurrences;
        start++;; // see the note
    }
    cout << occurrences << endl;
}

所以我尝试自己写一个函数,但是速度更慢:

#include<iostream>
#include<cstdio>
#include<string>
#include<queue>

using namespace std;

int main()
{
    int n, m;
    string to_find;
    queue<int> rada;  
    int occurrences = 0;
    cin >> m >> to_find >> n;
    for (int i = 0; i < n; i++)
    {
        char c;
        scanf(" %c", &c);
        int max = rada.size();
        for (int j = 0; j < max; j++)
        {
            int index = rada.front();
            rada.pop();
            if (c == to_find[index])  
            {
                if (++index == m) {
                    occurrences++;
                }
                else
                    rada.push(index);
            }
        }
        if (c == to_find[0])
        {
            if (1 == m)
                n++;
            else
                rada.push(1);
        }
    }
    cout << occurrences << endl;

}

我知道有些人在 0 毫秒内完成了这项工作,但我的第一个代码需要超过 2000 毫秒,而第二个代码比这要多得多。你有什么想法如何解决这个问题吗? 谢谢。

编辑: 长度限制:

M <= 100 000 - 子字符串的长度

N<= 200 000 - 基本字符串的长度

最佳答案

您提供的算法是 O(M*N),其中 N 是文本的长度,M 是搜索到的世界的长度。通常,库也会实现朴素算法。然而,Knuth、Morrison 和 Pratt 有一个算法,它在 O(M+N) 时间内完成。参见,例如,维基百科 Knuth-Morrison-Pratt Algorithm .它有一些变体可能更容易实现,例如 Boyer-Moore-Horsepool .

关于c++ - 查找子字符串出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34269206/

相关文章:

c++ - 在 Visual Studio 中编译 gcc 代码会导致错误 C3646 : '__attribute__' : unknown override specifier

c++ - 通过 copy 和 decltype 捕获 Lambda 引用

c++ - 模板化类型转换运算符和偏特化

java - 为什么 Java 在这种情况下比 C 更快(也更慢)?

Python - 性能问题 - 过滤 Pandas 数据框与过滤列表 dics vs numpy recs

android - 在 CustomView 中连续绘制时性能不佳

c++ - 逐字逐句阅读长文本

c++ - C++ 字符串文字转义字符的规则

python - 类型错误 : Can't convert 'bytes' object to str implicitly despite adding . 编码()到字符串

c++ - 带空字符的 STL basic_string 长度