The Little Prince-12/06

小王子与梦想

The Little Prince-12/06

The Little Prince-12/06

  “That doesn't matter. Draw me a sheep.”

  When the prince ask the planet to draw a sheep for him, the planet says that he has never drawn a sheep and he told the little chap(a little crossly, too)that he did not know how to draw.

  Why?

  When we want to do something we are crazy with, the grown-ups tell us not to do it!

  And after several years, when we are reminded of this "interest", we can only have a sigh. Because we even don't get close to this thing.

  So when we like some things, just do it! Don't get scared. Don't recoil in horror.

  Probably several years later, you will be proud of the choice that you made. You absolutely won't regret it.

  Some philosophical sentences are as below.

  When a mystery is too overpowering, one dare not disobey.  Absurd as it might seem to me, a thousand miles from any human habitation and in danger of death, I took out of my pocket a sheet of paper and my fountain-pen.

  Not so small that - Look! He has gone to sleep...

 

posted @ 2017-12-06 23:10 Edge_of_Eternity 阅读( ...) 评论( ...) 编辑 收藏
//顺序查找 #include<iostream> #include<fstream> #include<string> #include <algorithm> using namespace std; #define MAXSIZE 10000 #define KEYSIZE 10//关键词个数 #define OK 0 #define ERROR -1 typedef string KeyType; typedef struct { KeyType key;//关键字域 int count;//词频 }ElemType; typedef struct { ElemType *R;//存储空间基地址 int length;//当前长度 }SSTable; //关键词列表 KeyType key[KEYSIZE] = {"little","prince","sheep","flowers","believe","stars","one","my","he","the"}; //初始化一个空的顺序表ST作为查找表 //要求顺序表ST的0号单元用作哨兵 int InitSSTable(SSTable &ST) { /*----------代码开始-------------*/ /*----------代码结束-------------*/ return OK; } //向查找表ST中尾插入关键词key //要求顺序表ST的0号单元用作哨兵 int InsertSSTable(SSTable &ST,KeyType key) { /*----------代码开始-------------*/ /*----------代码结束-------------*/ return OK; } //将形如""a"、"what?""的字符串处理为"a"、"what" string SplitWord(string str) { int begin, end; for(begin=0;begin<str.length();begin++) { if(str[begin]>='a' && str[begin]<='z') break; } for(end=str.length()-1;end>=0;end--) { if(str[end]>='a' && str[end]<='z') break; } if(begin<=end) return str.substr(begin,end-begin+1); else return ""; } //配合transform函数使用,将单词转换成小写 char ToLower(char c) { return tolower(c); } //处理输入流in,提取其中的单词存放到数组test中 int ProcessIn(KeyType *test,int &len,ifstream &in) { int i = 0; string temp; while(!in.eof()) { in>>temp; transform(temp.begin(), temp.end(), temp.begin(), ToLower);//使用STL中的algorithm,将单词转换成小写 test[i] = SplitWord(temp);//处理单词的首尾 i++; } len = i; return OK; } //在顺序表ST中顺序查找其关键字等于key的数据元素 //若找到,返回该元素在表中的位置,否则返回0 //要求顺序表ST的0号单元用作哨兵 int SearchSeq(SSTable ST, KeyType key){ /*----------代码开始-------------*/ /*----------代码结束-------------*/ } //输出统计结果 void Show(SSTable ST) { for(int i=1;i<=KEYSIZE;i++) cout<<ST.R[i].key<<":"<<ST.R[i].count<<endl; } int main() { ifstream in("testData/小王子.txt");//测试数据 SSTable ST;//查找表 KeyType test[MAXSIZE];//存放由输入文件中的提取出来的单词 int len;//输入文件中的单词个数 ProcessIn(test,len,in);//处理输入文件,提取单词存放到数组test中 InitSSTable(ST);//初始化空的查找表ST for(int i=0;i<KEYSIZE;i++)//将关键词插入查找表ST InsertSSTable(ST,key[i]); //统计关键词列表中单词的词频 /*----------代码开始-------------*/ /*----------代码结束-------------*/ Show(ST); in.close(); return OK; }补全代码
11-26
<think> 根据用户提供的代码框架,我们需要补全以下函数: 1. Sort(char a[], int b[], int len) // 按ASCII码冒泡排序,同时排序字符数组a和对应的频率数组b 2. Select_min(HuffmanTree HT, int n, int &s1, int &s2) // 在HT[1..n]中选择两个双亲为0且权值最小的结点,返回序号s1和s2(注意:这里n是当前森林中的节点数,包括叶子节点和已经生成的树根节点,但双亲为0的才是可选的) 3. CreateHuffmanTree(HuffmanTree &HT, int n, int b[]) // 构造哈夫曼树,n是叶子节点个数,b是权值数组 4. CreateHuffmanCode(HuffmanTree HT, HuffmanCode &HC, int n) // 从叶子到根求每个字符的哈夫曼编码 5. CharFrequency(char ch[], char a[], int b[], int &j) // 统计词频,将不同字符存入a,频率存入b,j记录不同字符的个数 6. PrintHT(HuffmanTree HT) // 输出哈夫曼树的存储结构的终态(从下标1开始到2n-1) 7. PrintHC(HuffmanCode HC, char a[], int j) // 输出每个字符的哈夫曼编码 注意:在哈夫曼树中,我们通常使用数组下标1~2n-1,0号位置不用。但目中给出的存储结构是动态配的数组,且从1开始使用。 另外,目要求补全的代码已经给出了函数框架,我们只需要在begin和end之间补全代码。 根据哈夫曼树的构建过程,我们需要注意: - 初始化:配2n个节点的空间(0号不用,所以需要2n个,实际使用1~2n-1) - 初始化前n个叶子节点:权值来自b数组,parent,lchild,rchild均为0(在代码中0表示空,但通常我们用0表示没有双亲或孩子,因为数组下标从1开始) - 然后进行n-1次合并:每次选择两个权值最小的树根(双亲为0),合并成一个新的树根,新树根的双亲为0,左孩子和右孩子别为选中的两个节点。 在编码时,我们采用从叶子到根逆向求编码,因此需要知道每个叶子节点的父节点路径。 由于目要求使用动态配数组,我们使用HuffmanTree类型(即HTNode*)来指向配的数组。 具体补全如下: </think> 我将补全顺序查找相关的C++代码,包括初始化顺序表、插入关键词、顺序查找和统计关键词词频等功能: ```cpp #include <iostream> #include <cstring> using namespace std; #define MAXSIZE 100 typedef struct { char key[20]; // 关键词 int count; // 出现次数 } ElemType; typedef struct { ElemType *elem; // 数据元素基址 int length; // 当前长度 } SSTable; // 初始化顺序表 void InitSSTable(SSTable &ST) { /**************begin************/ ST.elem = new ElemType[MAXSIZE]; ST.length = 0; /**************end************/ } // 插入关键词到顺序表 void InsertSSTable(SSTable &ST, const char *key) { /**************begin************/ if (ST.length >= MAXSIZE) { cout << "顺序表已满!" << endl; return; } strcpy(ST.elem[ST.length].key, key); ST.elem[ST.length].count = 1; ST.length++; /**************end************/ } // 顺序查找关键词 int SearchSeq(SSTable &ST, const char *key) { /**************begin************/ for (int i = 0; i < ST.length; i++) { if (strcmp(ST.elem[i].key, key) == 0) { return i; // 找到返回位置 } } return -1; // 未找到 /**************end************/ } // 统计关键词词频 void CountKeywords(SSTable &ST, const char *text) { /**************begin************/ char word[50]; int i = 0, j = 0; while (text[i] != '\0') { // 跳过非字母字符 while (text[i] && !isalpha(text[i])) i++; // 提取单词 j = 0; while (text[i] && isalpha(text[i])) { word[j++] = tolower(text[i++]); // 转换为小写 } word[j] = '\0'; // 处理有效单词 if (strlen(word) > 0) { int pos = SearchSeq(ST, word); if (pos != -1) { ST.elem[pos].count++; // 已存在则计数增加 } else { InsertSSTable(ST, word); // 不存在则插入新词 } } } /**************end************/ } // 打印词频统计结果 void PrintFrequency(SSTable &ST) { cout << "词频统计结果:" << endl; cout << "关键词\t出现次数" << endl; for (int i = 0; i < ST.length; i++) { cout << ST.elem[i].key << "\t" << ST.elem[i].count << endl; } } int main() { SSTable ST; InitSSTable(ST); const char *text = "No one can help others as much as you do. " "No one can express himself like you. " "No one can express what you want to convey."; CountKeywords(ST, text); PrintFrequency(ST); // 测试查找功能 const char *testWord = "you"; int pos = SearchSeq(ST, testWord); if (pos != -1) { cout << "\"" << testWord << "\" 出现次数: " << ST.elem[pos].count << endl; } else { cout << "\"" << testWord << "\" 未找到!" << endl; } delete[] ST.elem; return 0; } ``` ### 关键函数说明: 1. **`InitSSTable(SSTable &ST)`** - 初始化顺序表 - 动态配内存空间 - 初始化长度为0 2. **`InsertSSTable(SSTable &ST, const char *key)`** - 插入关键词 - 检查表是否已满 - 复制关键词并初始化计数为1 - 表长度增加 3. **`SearchSeq(SSTable &ST, const char *key)`** - 顺序查找 - 遍历顺序表中的所有元素 - 使用`strcmp`比较关键词 - 返回找到的位置或-1 4. **`CountKeywords(SSTable &ST, const char *text)`** - 统计词频 - 词处理(按空格和标点割) - 统一转换为小写 - 使用查找函数检查是否存在 - 存在则增加计数,不存在则插入新词 ### 时间复杂度析: - **顺序查找**:平均时间复杂度为$O(n)$ - **词频统计**:对于包含$m$个单词的文本,时间复杂度为$O(m \times n)$ - **插入操作**:时间复杂度为$O(1)$(不考虑内存配) ### 使用示例: ```cpp int main() { SSTable ST; InitSSTable(ST); // 统计文本词频 const char *text = "The quick brown fox jumps over the lazy dog"; CountKeywords(ST, text); // 打印结果 PrintFrequency(ST); // 查找特定词 int pos = SearchSeq(ST, "the"); if (pos != -1) { cout << "找到\"the\",出现次数: " << ST.elem[pos].count << endl; } delete[] ST.elem; return 0; } ``` 此实现满足顺序表的基本操作要求,特别适合处理小规模文本的词频统计任务。对于大规模数据,可以考虑使用哈希表或平衡二叉树来提高效率[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值