时间超限,请问代码应该如何优化

乐百川 2014-05-03 08:18:56
问题在此
随着互联网技术的飞速发展,如何从海量数据中查找所需内容,不仅是科研人员关注的热点问题,许多IT公司也先后推出了各自的搜索引擎,如:Google、百度、Bing等。搜索引擎的核心是如何对Web网页构建有效的索引,以便能够快速查找和匹配查询关键词,并及时地将搜索结果返回给用户。
在这个实验中,请实现一个英文单词的二叉查找树,并可根据输入的英文单词进行搜索,同时可给出单词出现的次数。具体要求如下:
(1)构造二叉查找树:
①读入文本内容,过滤掉阿拉伯数字和标点符号,并将英文字母的大写形式全部转换成小写形式。
②按照英文字母表的顺序构造英文单词的二叉查找树。当两个英文单词的首字母相同时,按第二个字母进行排序,依次类推。
③当待插入的单词已在二叉查找树中,则将该单词的出现次数增1。
(2)遍历二叉查找树:
①搜索:输入一个待检索单词,在二叉查找树中进行查找,如果能找到该单词,则输出该单词及其出现次数;
②实现二叉查找树的中序遍历,并将遍历结果输出到屏幕上。
(3)删除结点:给定一个停用词列表(停用词是指对搜索没有作用的词,如:of, and, a, an, the等等),将二叉查找树中的属于停用词表中的单词依次删除(不仅删除结点,还需清空记录该单词位置信息的单链表)。
Input
输入有以下四种情况:
当输入大写英文字母'T'时,表示下一行是文本内容,包含若干英文单词、标点符号以及阿拉伯数字,用于构建二叉查找树。文本内容以字符‘@’结束,文本中的每个英文单词的长度不超过30个字母。
当输入大写英文字母'S'时,表示后面跟着的若干行都是停用词,每个停用词占一行,当某行是字符‘#’时,表示停用词输入完毕。对每个停用词,都需要删除二叉查找树中的相应结点,即:每输入一个停用词,执行一次删除结点的操作。
当输入大写英文字母'V'时,表示中序遍历二叉查找树。遍历结果中的每个单词占一行,先输出该单词,然后输出一个空格,再输出该单词出现的次数。
当输入大写英文字母'Q'时,表示后面跟着的若干行都是查询词,每个查询词占一行,当某行是字符‘#’时,表示查询词输入完毕。对每个查询词,都需要在二叉查找树中的搜索相应结点,如果找到,则输出该单词及其出现次数,如果未找到,则输出-1。每个查询词的查询结果占一行,先输出该单词,然后输出一个空格,再输出该单词出现的次数。
当输入大写英文字母'X'时,表示输入结束。
Output
按照输入的要求输出相应结果。提示:只有在'V'或者'Q'状态下,才有内容需要输出。
Sample Input
T According to characteristics of Mongolian word formation, a method for removing inflectional suffixes from word images of the Mongolian Kanjur is proposed in this paper. @ V Q the # S after against all almost also although among an and # Q the # S the their then there therefore these they this those though three to two # Q the # V T This paper presents a new method to recognize machine printed traditional Mongolian characters by using back propagation (BP) neural networks. @ Q the # V Q mongolian ocr # X
Sample Output
a 1 according 1 characteristics 1 for 1 formation 1 from 1 images 1 in 1 inflectional 1 is 1 kanjur 1 method 1 mongolian 2 of 2 paper 1 proposed 1 removing 1 suffixes 1 the 1 this 1 to 1 word 2 the 1 the 1 -1 a 1 according 1 characteristics 1 for 1 formation 1 from 1 images 1 in 1 inflectional 1 is 1 kanjur 1 method 1 mongolian 2 of 2 paper 1 proposed 1 removing 1 suffixes 1 word 2 -1 a 2 according 1 back 1 bp 1 by 1 characteristics 1 characters 1 for 1 formation 1 from 1 images 1 in 1 inflectional 1 is 1 kanjur 1 machine 1 method 2 mongolian 3 networks 1 neural 1 new 1 of 2 paper 2 presents 1 printed 1 propagation 1 proposed 1 recognize 1 removing 1 suffixes 1 this 1 to 1 traditional 1 using 1 word 2 mongolian 3 -1



#include <iostream>
#include <cctype>
#include <string>
#include <cstdio>
using namespace std;
class node
{
public:
node(string str = "") :word(str), left(NULL), right(NULL), count(1)
{

}
string word;
node *left;
node *right;
int count;
};
class BSTree
{
public:
BSTree() :root(NULL)
{
}
node *find(const string&, node *&);
void insert(const string &);
void del(const string &);
void search(const string &str);
void print(node *);
void printTree()
{
print(root);
}
private:
node *root;
};
void BSTree::search(const string &str)
{
node *pp, *p;
p = find(str, pp);
if (!p)
{
printf("-1\n");
}
else
{
printf("%s %d\n", p->word.c_str(), p->count);
}
}
node *BSTree::find(const string &str, node *&parent)
{
node *p = root;
while (p && (str != p->word))
{
parent = p;
if (str < p->word)
{
p = p->left;
}
else if (str>p->word)
{
p = p->right;
}
}
return p;
}
void BSTree::insert(const string &str)
{
node *pp = NULL, *p;
if ((p = find(str, pp)) != NULL)
{
p->count += 1;
return;
}
node *r = new node(str);
if (!root)
{
root = r;
}
else if (str < pp->word)
{
pp->left = r;
}
else
{
pp->right = r;
}
}
void BSTree::del(const string &str)
{
node *p = NULL, *pp = NULL;
if ((p = find(str, pp)) == NULL)
{
return;
}
if (p->left && p->right)
{
node *s = p->right, *ps = p;
while (s->left)
{
ps = s;
s = s->left;
}
p->word = s->word;
p->count = s->count;
p = s;
pp = ps;
}
node *pc;
if (p->left)
{
pc = p->left;
}
else
{
pc = p->right;
}
if (p == root)
{
root = pc;
}
else
{
if (p == pp->left)
{
pp->left = pc;
}
else
{
pp->right = pc;
}
}
delete p;
}
void BSTree::print(node *h)
{
if (!h)
{
return;
}
print(h->left);
printf("%s %d\n", h->word.c_str(), h->count);
print(h->right);
}
string convert(const string &str)
{
string s;
for (int i = 0; i < str.length(); ++i)
{
if (isalpha(str[i]))
{
if (isupper(str[i]))
{
s.push_back(tolower(str[i]));
}
else
{
s.push_back(str[i]);
}
}
}
return s;
}
int main()
{
BSTree tree;
string s;
char c;
while ((c = cin.get()) != 'X')
{
cin.get();
switch (c)
{
case 'T':
getline(cin, s, ' ');
while (s != "@")
{
tree.insert(convert(s));
getline(cin, s, ' ');
}
break;
case 'S':
getline(cin, s, ' ');
while (s != "#")
{
tree.del(convert(s));
getline(cin, s, ' ');
}
break;
case 'V':
tree.printTree();
break;
case 'Q':
getline(cin, s, ' ');
while (s != "#")
{
tree.search(convert(s));
getline(cin, s, ' ');
}
break;
}
}
return 0;
}


代码分析如下,那个占99%的函数貌似是cin.get,然后我就什么也不知道了
...全文
299 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
乐百川 2014-05-04
  • 打赏
  • 举报
回复
引用 2 楼 u012061345 的回复:
知道有多少个单词吗?不要使用new和delete动态分配内存。 使用静态数组模拟缓存池,去实现二叉树。 OJ上一般都这么干。
我有点不太理解…… 有二十来个单词吧,怎么用静态数组模拟呢,能不能详细一点……
赵4老师 2014-05-04
  • 打赏
  • 举报
回复
乍看起来c++的cin、cout在输入、输出上比c的scanf、printf简单,不用格式控制符! 但是不用格式控制符,输入输出恰好是你期望的格式的时候好说;等到输入输出不是你期望的格式的时候,你就会觉得还是用格式控制符更方便、更靠谱。 摒弃cin、cout! 使用scanf、printf。
一起来玩玩呗 2014-05-04
  • 打赏
  • 举报
回复
cin>> get () 只接受一个字符
乐百川 2014-05-04
  • 打赏
  • 举报
回复
引用 4 楼 u012061345 的回复:
[quote=引用 3 楼 u011054333 的回复:] [quote=引用 2 楼 u012061345 的回复:] 知道有多少个单词吗?不要使用new和delete动态分配内存。 使用静态数组模拟缓存池,去实现二叉树。 OJ上一般都这么干。
我有点不太理解…… 有二十来个单词吧,怎么用静态数组模拟呢,能不能详细一点……[/quote] 20个?!算了,别优化了。怎么搞都一样。 超时应该是输入弄错了。自己调调。[/quote] 我把所有输入都改成cin这下不是时间超限了,变成答案错误了,不过我和标准输出一个一个对照过了呀,算了,就这样吧
罗博士 2014-05-04
  • 打赏
  • 举报
回复
引用 3 楼 u011054333 的回复:
[quote=引用 2 楼 u012061345 的回复:] 知道有多少个单词吗?不要使用new和delete动态分配内存。 使用静态数组模拟缓存池,去实现二叉树。 OJ上一般都这么干。
我有点不太理解…… 有二十来个单词吧,怎么用静态数组模拟呢,能不能详细一点……[/quote] 20个?!算了,别优化了。怎么搞都一样。 超时应该是输入弄错了。自己调调。
罗博士 2014-05-03
  • 打赏
  • 举报
回复
知道有多少个单词吗?不要使用new和delete动态分配内存。 使用静态数组模拟缓存池,去实现二叉树。 OJ上一般都这么干。
乐百川 2014-05-03
  • 打赏
  • 举报
回复

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧