字典树 插入 排序 分配id
来源:互联网 发布:冬天日本哪里好玩 知乎 编辑:程序博客网 时间:2024/06/06 15:49
字典树分配id最常见,其他一半很少用,也可能渣做的题比较少
贴代码,代码有解释:
#include <cstdio>#include <cstring>#include <algorithm>#include <string>using namespace std;int cnt;struct node{ int id; int prefix;//记录前缀个数 node *next[26];//指向字典树下一层,如果全是小写字母是26这视情乱而定 node(){ for(int i=0;i<26;i++) next[i]=NULL; id =0; prefix=0; }//初始化}*root;int Insert(char *s){ int len =strlen (s); node *p= root; for(int i=0;i<len;i++){ int k=s[i]-'a'; if(p->next[k]==NULL){ p->next[k]=new node(); } p=p->next[k]; p->prefix++; } if(p->id==0) return p->id=++cnt; return p->id;}//插入并分配idint find(char s[]){ int len =strlen(s); node *p =root; for(int i=0;i<len;i++){ p=p->next[s[i]-'a']; } return p->prefix;}//查找此前缀是多少人的前缀int x;//记录总共有多少字符串参与了排序int all[200];//记录排好序的idvoid output(node *p){ if(p!=NULL){ if(p->id!=0){ all[x++]=p->id; } for(int i=0;i<26;i++){ if(p->next[i]){ output(p->next[i]); } } }}//字符串排序int main(){ cnt =0; root =new node(); char s[100][100]; char s2[100][100]; int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s",s[i]); printf("%d\n",Insert(s[i])); strcpy(s2[Insert(s[i])],s[i]); } for(int i=0;i<n;i++){ printf("%d\n",find(s[i])); } /*排序后*/ x=0; output(root); printf("%d\n",x); for(int i=0;i<x;i++){ printf("%d %s\n",all[i],s2[all[i]]); }}
0 0
- 字典树 插入 排序 分配id
- ID Codes(字典序排序)
- 51nod 1526 分配笔名(字典树)
- hdu2846 字典树(带id的)
- 字典树 一种快速插入查询数据结构
- 字典树的插入 删除 查找
- 字典树的建立,插入,查找
- 经典排序算法设计与分析(插入排序、冒泡排序、选择排序、shell排序、快速排序、堆排序、分配排序、基数排序、桶排序、归并排序)
- 经典排序算法设计与分析(插入排序、冒泡排序、选择排序、shell排序、快速排序、堆排序、分配排序、基数排序、桶排序、归并排序)
- 字典排序
- 字典排序
- 字典排序
- 字典排序
- 字典排序
- 字典排序
- 字典排序
- 字典排序
- 字典排序
- Axis 1.4 WSDL2Java 的使用流程
- JavaBean类初学
- HDU 4452 - Running Rabbits(模拟)
- 微软Xbox击败PS4 主导“黑五”游戏机市场
- jQueryEasyUI Messager基本使用
- 字典树 插入 排序 分配id
- iOS 自定义相册图片编辑页面
- 在Node.js中使用TCP套接字编程
- C++ #include <iostream> #include <iostream.h> #include <string.h>区别及作用
- 英特尔将为谷歌眼镜提供芯片 扩大可穿戴市场
- 查询域名是否被墙
- iOS 画出不规则图形
- ActiveX控件的注册和反注册
- 迄今为止最优的Eclipse运行性能调优 ,含eclipse.ini