关于字典树的有关问题,代码基本无错,也能跑起来,但是有一点点小疑问
关于字典树的问题,代码基本无错,也能跑起来,但是有一点点小疑问
题就是::::在main函数里开始检索后面的for (int i = 0; i < count; i++)
为什么必须加上int,不然就会输出烫烫烫烫
明明赋值了全局变量,也重新赋值0了啊,后来为什么会莫名其妙的变成26了??
加上int就完全正确了
请指点指点咯
#include <iostream>
using namespace std;
const int branchNum = 26;
int i;
struct TNode
{
bool isStr;
TNode * next[branchNum];
TNode();
};
TNode::TNode()
{
isStr = false;
for (i = 0; i < branchNum; i++)
{
next[i] = NULL;
}
}
class Trie
{
public:
Trie();
void Insert(char *word);
int Search(char *word, int length);
void DeleteTrie(TNode * root);
void refreshTrie();
private:
TNode * root;
};
Trie::Trie()
{
root = new TNode;
}
void Trie::Insert(char * word)
{
TNode * location = root;
int pos;
while(*word)
{
if (*word >= 'a' && *word <= 'z')
pos = *word - 'a';
else if (*word >= 'A' && *word <= 'Z')
pos = *word - 'A';
else
return;
if (location->next[pos] == NULL)
{
TNode *tem = new TNode;
location->next[pos] = tem;
}
location = location->next[pos];
word++;
}
location->isStr = true;
}
int Trie::Search(char *word, int length)
{
TNode * location = root;
int len,num;
len = num = 0;
while(*word)
{
if (location->next[*word - 'a'])
{
location = location->next[*word -'a'];
len++;
if (len = length && location->isStr == true)
{
num++;
len = 0;
location = root;
}
word++;
}
else
{
if (len == 0)
word++;
else
{
word = word -len + 1;
len = 0;
location = root;
}
}
}
return num;
}
void Trie::DeleteTrie(TNode * root)
{
for (i = 0; i < branchNum; i++)
{
if (root->next[i] != NULL)
{
DeleteTrie(root->next[i]);
}
}
delete root;
}
void Trie::refreshTrie()
{
DeleteTrie(root);
root = new TNode;
}
int main(void)
{
Trie t;
char text[20000];
char sort[10][10] = {0};
cout<<"请输入要检索的文章 : ";
cin>>text;
int count;
int num = 0;
cout<<"请输入要检索的次数 : ";
cin>>count;
for (i = 0; i < count; i++)
{
cout<<"请输入要检索的关键字 : "<<endl;
cin>>sort[i];
}
cout<<"开始检索。。。"<<endl;
for (int i = 0; i < count; i++)
{
t.Insert(sort[i]);
int tem = t.Search(text, strlen(sort[i]));
if (tem)
cout<<"此文章含有关键字:"<<sort[i]<<"共出现"<<tem<<"次"<<endl;
else
cout<<"此文章不含有关键字:"<<sort[i]<<endl;
}
t.refreshTrie();
system("pause");
return 0;
}
问题就是::::在main函数里开始检索后面的for (int i = 0; i < count; i++)
为什么必须加上int,不然就会输出烫烫烫烫
明明赋值了全局变量,也重新赋值0了啊,后来为什么会莫名其妙的变成26了??
加上int就完全正确了
请指点指点咯
------解决方案--------------------
LZ用i这个变量用的太多了,
main函数里面的第二个for在使用:
题就是::::在main函数里开始检索后面的for (int i = 0; i < count; i++)
为什么必须加上int,不然就会输出烫烫烫烫
明明赋值了全局变量,也重新赋值0了啊,后来为什么会莫名其妙的变成26了??
加上int就完全正确了
请指点指点咯
#include <iostream>
using namespace std;
const int branchNum = 26;
int i;
struct TNode
{
bool isStr;
TNode * next[branchNum];
TNode();
};
TNode::TNode()
{
isStr = false;
for (i = 0; i < branchNum; i++)
{
next[i] = NULL;
}
}
class Trie
{
public:
Trie();
void Insert(char *word);
int Search(char *word, int length);
void DeleteTrie(TNode * root);
void refreshTrie();
private:
TNode * root;
};
Trie::Trie()
{
root = new TNode;
}
void Trie::Insert(char * word)
{
TNode * location = root;
int pos;
while(*word)
{
if (*word >= 'a' && *word <= 'z')
pos = *word - 'a';
else if (*word >= 'A' && *word <= 'Z')
pos = *word - 'A';
else
return;
if (location->next[pos] == NULL)
{
TNode *tem = new TNode;
location->next[pos] = tem;
}
location = location->next[pos];
word++;
}
location->isStr = true;
}
int Trie::Search(char *word, int length)
{
TNode * location = root;
int len,num;
len = num = 0;
while(*word)
{
if (location->next[*word - 'a'])
{
location = location->next[*word -'a'];
len++;
if (len = length && location->isStr == true)
{
num++;
len = 0;
location = root;
}
word++;
}
else
{
if (len == 0)
word++;
else
{
word = word -len + 1;
len = 0;
location = root;
}
}
}
return num;
}
void Trie::DeleteTrie(TNode * root)
{
for (i = 0; i < branchNum; i++)
{
if (root->next[i] != NULL)
{
DeleteTrie(root->next[i]);
}
}
delete root;
}
void Trie::refreshTrie()
{
DeleteTrie(root);
root = new TNode;
}
int main(void)
{
Trie t;
char text[20000];
char sort[10][10] = {0};
cout<<"请输入要检索的文章 : ";
cin>>text;
int count;
int num = 0;
cout<<"请输入要检索的次数 : ";
cin>>count;
for (i = 0; i < count; i++)
{
cout<<"请输入要检索的关键字 : "<<endl;
cin>>sort[i];
}
cout<<"开始检索。。。"<<endl;
for (int i = 0; i < count; i++)
{
t.Insert(sort[i]);
int tem = t.Search(text, strlen(sort[i]));
if (tem)
cout<<"此文章含有关键字:"<<sort[i]<<"共出现"<<tem<<"次"<<endl;
else
cout<<"此文章不含有关键字:"<<sort[i]<<endl;
}
t.refreshTrie();
system("pause");
return 0;
}
问题就是::::在main函数里开始检索后面的for (int i = 0; i < count; i++)
为什么必须加上int,不然就会输出烫烫烫烫
明明赋值了全局变量,也重新赋值0了啊,后来为什么会莫名其妙的变成26了??
加上int就完全正确了
请指点指点咯
------解决方案--------------------
LZ用i这个变量用的太多了,
main函数里面的第二个for在使用:
- C/C++ code
t.Insert(sort[i]); int tem = t.Search(text, strlen(sort[i]));