小编Ind*_*mer的帖子

查找给定字符串中的所有重复子字符串

我刚接触到一个采访问题:找到一个给定字符串中所有重复的子字符串,其最小大小为2.该算法应该是高效的.

上面给出了上述问题的代码,但效率不高.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <set>
#include <string>

using namespace std;

int main()
{
    typedef string::const_iterator iterator;
    string s("ABCFABHYIFAB");
    set<string> found;

    if (2 < s.size())
        for (iterator i = s.begin() + 1, j = s.end(); i != j; ++i)
            for (iterator x = s.begin(); x != i; ++x)
            {
                iterator tmp = mismatch(i, j, x).second;;
                if (tmp - x > 1)
                    found.insert(string(x, tmp));
            }

            copy(found.begin(), found.end(),ostream_iterator<string>(cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,是否有任何数据结构可以在O(N)的时间复杂度上实现上述问题?

如果您的答案是后缀树或哈希,请详细说明.

c++ string algorithm

13
推荐指数
1
解决办法
1万
查看次数

正常功能和模板功能之间的优先级

在下面的代码中,main函数使用普通函数而不是Template函数.

#include <iostream>

using namespace std;

template <class T>
void num(T t){cout<<"T : "<<t;}

void num(int a){cout<<"wT : "<<a;}


int main()
{
    num(5);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这背后可能的原因是什么?

c++ templates overloading

6
推荐指数
2
解决办法
1932
查看次数

程序可执行文件标记为只读

我正在阅读关于分段错误的维基百科,并通过以下代码和语句来完成.

int main(void)
 {
     char *s = "hello world";
     *s = 'H';
 }
Run Code Online (Sandbox Code Playgroud)

当编译包含此代码的程序时,字符串"hello world"被放置在标记为只读的程序可执行文件的部分中; 加载时,操作系统将其与其他字符串和常量数据放在只读的内存段中.执行时,变量s设置为指向字符串的位置,并尝试通过变量将H字符写入内存,从而导致分段错误.使用编译器编译此类程序,该编译器不会在编译时检查只读位置的分配.

我的问题是文件权限,即当可执行文件标记为只读和读写时等等?

我想知道关于文件权限的所有信息.我们可以明确地更改文件权限吗?

c file-permissions segmentation-fault

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