小编Cor*_*zin的帖子

为什么Regex(c ++)采用指数时间?

我正在从教科书中做一些正则表达式问题,他们会阅读以下内容:

"[匹配]从行开头开始的所有字符串都带有一个整数,并以一个单词结束在该行的末尾."

我为此写了以下正则表达式:

^[0-9]+\s.*+\b[a-zA-Z]+$
Run Code Online (Sandbox Code Playgroud)

但是,当我使用以下代码在C++中实现它时:

#include <iostream>
#include <string>
#include <regex>
#include <time.h>

int main(){
    clock_t t;
    bool match;
    std::string exp = "^[0-9]+\\s.*+\b[a-zA-Z]+$";
    std::string str = "1 a few words 1";
    std::string s (str);
    std::smatch m;
    std::regex e (exp);
    while (true){
        t = clock();
        match = std::regex_match(s, m, e); 
        s = s + "1";
        std::cout << clock() - t << std::endl;
    }   
}
Run Code Online (Sandbox Code Playgroud)

每次迭代所花费的CPU时间是:

1 1181529
2 3398674
3 10102763
4 30370932
5 92491242
Run Code Online (Sandbox Code Playgroud)

看起来很复杂 O( 3^n )

为什么会这样?在表达中有什么我做错了吗?

如果我使用像"1 …

c++ regex clock time-complexity

8
推荐指数
1
解决办法
457
查看次数

来自未知方搜狗的请求

我在 Amazon Lightsail 上托管一个简单的原型,并且在 Django 服务器上看到了一些奇怪的请求。有什么需要担心的吗?

Invalid HTTP_HOST header: 'fuwu.sogou.com'. You may need to add 'fuwu.sogou.com' to 
ALLOWED_HOSTS.
Invalid HTTP_HOST header: 'fuwu.sogou.com'. You may need to add 'fuwu.sogou.com' to 
ALLOWED_HOSTS.
Bad Request: /http:/fuwu.sogou.com/404/index.html
Bad Request: /http:/fuwu.sogou.com/404/index.html
[01/Aug/2021 02:50:44] "GET http://fuwu.sogou.com/404/index.html HTTP/1.1" 400 63056
[01/Aug/2021 02:50:44] "GET http://fuwu.sogou.com/404/index.html HTTP/1.1" 400 63066
[01/Aug/2021 02:50:51] code 400, message Bad request syntax ('\x05\x01\x00')
[01/Aug/2021 02:50:51] "" 400 -
Run Code Online (Sandbox Code Playgroud)

security django malware

6
推荐指数
1
解决办法
1058
查看次数

标签 统计

c++ ×1

clock ×1

django ×1

malware ×1

regex ×1

security ×1

time-complexity ×1