【发布时间】:2019-07-08 04:27:20
【问题描述】:
我正在解决DISTINCT SUBSTRING(给定一个字符串,我们需要找到它的不同子字符串的总数)。
我正在使用 Trie 后缀来解决它。
我正在通过测试用例,但在提交时收到TLE。另外,占用的空间非常大,在4093M。
注意:由于总共可以有 256 个字符,所以我将数组大小设置为 257, ascii 值作为索引。
我现在在想什么:
for(int i=0;i<p;i++){
string temp = str.substr(i,p-i);
insert1(root,temp);
}
由于substr() 可能需要 O(n) 时间,在最坏的情况下插入函数也需要
(n) 最坏情况下的时间,O(n) for 循环:O(n^3)。这让我得到了 TLE。
错误:无法将 'temp' 从 'std::__cxx11::string* {aka std::__cxx11::basic_string*}' 转换为 'std::__cxx11::string {aka std::__cxx11::基本字符串}'| ||=== 构建失败:2 个错误,0 个警告(0 分钟,0 秒)===|
所以我想用这样的东西替换substr():
for(int i=0;i<p;i++){
string *temp = &str[i];
insert1(root,temp);
} ///and it is giving me error please suggest here what is the mistake and what to do
这样我可以节省 O(n) 时间。
请告诉我如何修改我的 trie 方法以使其被接受。
#include<iostream>
#include<string>
using namespace std;
const int alphabetsize = 257;
int cnt=0;
struct trienode{
struct trienode* children[alphabetsize];
bool isendofword;
};
struct trienode *getnode(void){
struct trienode *newnode = new trienode;
newnode->isendofword = false;
for(int i=0;i<alphabetsize;i++){
newnode->children[i]=NULL;
}
return newnode;
}
void insert1(struct trienode* root,string &key){
struct trienode *temp = root;
for(int i=0;i<key.length();i++){
int index = key[i];
if(!temp->children[index]){
temp->children[index]=getnode();
cnt++;
}
temp = temp->children[index];
}
temp->isendofword=true;
}
int main(){
int t;
cin>>t;
while(t--){
cnt=0;
string str;
cin>>str;
int p = str.length();
struct trienode* root = getnode();
for(int i=0;i<p;i++){
string temp = str.substr(i,p-i);
insert1(root,temp);
}
cout<<cnt<<endl;
}
}
【问题讨论】:
-
首先,我认为循环的复杂度永远不会是 O(n^3),而是 O(n^2)。另外,尝试第二种方法时遇到的错误是什么?
-
error: could not convert 'temp' from 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' to 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| -
在将指针传递给字符串后,大多数函数都不能在字符串上工作,例如 key.length() 或 (*key).length() 给出错误
-
我想你应该使用更高级的数据结构来解决这个问题。后缀数组似乎是一个不错的选择。但是,如果您只想通过测试,则可以通过比较字符串哈希值而不是
O(n^2)中的字符串值来解决它。
标签: c++ algorithm substring trie