不定长串的字典排序
来源:互联网 发布:网络实践教程全部答案 编辑:程序博客网 时间:2024/06/11 17:38
#include <iostream>#include <string>#include <queue>#include <vector>using namespace std;typedef string *pString;typedef queue<pString> StrBucket;/*变长的字符串的字典排序输入:strings:有字符串组成的数组,待排序strCnt:strings中的字符串的个数m:每个字符的范围在0-m之间输出:计算之后得到的strings就是排序后的字符串数组*/void VaryLenLexicographicSort(string * strings, int strCnt, int m = 256){unsigned int maxlength = 0;for (int i = 0; i < strCnt; i++){if (maxlength<strings[i].length()){maxlength = strings[i].length();}}//分门别类,长度一样的在一块vector<pString> *length = new vector<pString>[maxlength + 1];//根据这里的顺序,来找到桶里的值,this is keyvector<char> *nonempty = new vector<char>[maxlength];for (int i = 0; i < strCnt; i++){//length 编号 ==value的地址的字符串长度//length[3]=0x38922;0x38922->"abc";length[strings[i].length()].push_back(&strings[i]);for (int j = 0; j < strings[i].length(); j++){nonempty[j].push_back(strings[i].at(j));}}//打印nonempty的for (int i = 0; i < maxlength ;i++){cout << i<< " ";for (auto ib = nonempty[i].cbegin(); ib != nonempty[i].cend();ib++){cout << *ib<<" " ;}cout << endl;}queue<pString> q,qtemp;StrBucket *buckets = new StrBucket[m];for (int i = maxlength-1; i >=0; i--){//长度从大到小,进行操作for (int x = 0; x < length[i + 1].size();x++){cout << *length[i + 1].at(x) << endl;q.push(length[i + 1].at(x));}//先处理最长的,放到桶里while (!q.empty()){pString ps = q.front();//索引值:最长的从最后一个字符开始,压入其地址buckets[ps->at(i)].push(ps);q.pop();}//nonempty[i]有序sort(nonempty[i].begin(), nonempty[i].end());//打印nonempty的for (int index = 0; index < maxlength; index++){cout << index << " ";for (auto ib = nonempty[index].cbegin(); ib != nonempty[index].cend(); ib++){cout << *ib << " ";}cout << endl;}//根据nonempty[i]的顺序,来找到桶里的值,this is keyfor (int j = 0; j < nonempty[i].size(); j++){int val = nonempty[i].at(j);while (!buckets[val].empty()){pString ps = buckets[val].front();q.push(ps);buckets[val].pop();}}qtemp = q;while (!qtemp.empty()){cout << *qtemp.front() << endl;qtemp.pop();}}cout << "结果:" << endl;while (!q.empty()){cout << *(q.front()) << endl;q.pop();}if (length!=nullptr){delete[] length;}if (nonempty!=nullptr){delete[] nonempty;}if (buckets!=nullptr){delete[] buckets;}}void testVaryLenLexicographicSort(){string *strings = new string[7];strings[0] = "abc";strings[1] = "bac";strings[2] = "ab";strings[3] = "cba";strings[4] = "ba";strings[5] = "bbc";strings[6] = "c";for (int i = 0; i < 7;i++){cout << strings[i].length()<<" "<<strings[i] << endl;}VaryLenLexicographicSort(strings, 7);}void main(){testVaryLenLexicographicSort();cin.get();}
0 0
- 不定长串的字典排序
- hpu 2647 拓扑排序 + vector不定长数组的使用方法
- Python的不定长参数
- 不定长参数函数的实现
- java不定长参数的简单使用
- java不定长参数的简单使用
- Python函数的不定长参数
- Python的不定长参数研究
- console 接受不定长的用户输入
- c语言的不定长数组
- java不定长参数的简单使用
- 不定长数组vector,STL的使用
- Python函数的不定长参数
- 不定长顺序表的基本操作
- Java的不定长参数和Python的不定长参数对比
- 不定长参数测试
- TVarRec不定长数组
- 不定长数组:vector
- Jquery实现的一个DIV层面板的折叠/展开效果
- skb_buff 详解(二)
- Asus Other Hardware\ Asus Support Device一直提醒更新问题
- 怎样在Ubuntu14.04/16.04上安装Wordpress
- 【问题】开发板挂载NFS失败
- 不定长串的字典排序
- Ansys Icepak的CAE协同设计
- iOS标准时间与时间戳相互转换
- 最神秘的大数据公司Palantir
- 短文了解系统dubbo客户端通过zk调用另一个系统的dubbo服务端
- 24. Swap Nodes in Pairs*
- 毅力的真谛就是战胜自己
- 360手机助手2016年度嗨APP全民榜入围名单
- IOS中Json解析的四种方法