字典树模板
2015-08-01 10:10
246 查看
字典树:
用于统计,排序和保存大量的字符串(但不仅限于字符串)。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
3个基本性质:根节点不包含字符,除根节点外每一个节点都只包含一个字符; 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串; 每个节点的所有子节点包含的字符都不相同。
其基本操作有:查找、插入和删除,当然删除操作比较少见。
基本模板:
What Are You Talking About
解题思路:
结构体里面放字符串,每个节点是存的是火星文,然后火星文的最后一个单词放英文单词。查找的时候如果存在就输出英文单词,如果不存在就原样输出。
代码写的很丑就不放了。
Hat’s Words
标准模板题:
Immediate Decodability
用于统计,排序和保存大量的字符串(但不仅限于字符串)。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
3个基本性质:根节点不包含字符,除根节点外每一个节点都只包含一个字符; 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串; 每个节点的所有子节点包含的字符都不相同。
其基本操作有:查找、插入和删除,当然删除操作比较少见。
基本模板:
[code]//定义结构体 struct Node { struct Node *next[MAX];//指向下一个的结构体,MAX根据实际情况 bool flag;//该节点是否有单词 char str;//该节点存储的字符串 //根据题目需要可以定义其他类型 //定义的时候就清除; Node() { for(int i=0;i<MAX;i++) next[i]=NULL; flag = false; str[0]='\0'; } }; struct Node *root = new struct Node;//建立根节点 void Insert(char *str)//插入单词 { struct Node *cur = root; for(int i=0;str[i];i++) { if(cur->next[str[i]-'a']==NULL) { struct Node *newnode = new struct Node; cur->next[str[i]-'a'] = newnode; } cur = cur->next[str[i]-'a']; } cur->flag=true; strcpy(cur->str,str); return; } bool Query(char *str)//查询单词 { struct Node *cur=root; for(int i=0;str[i];i++) { if(cur->next[str[i]-'a']!=NULL) { cur=cur->next[str[i]-'a']; } else return false;//一旦没有其他的节点就return false; } return true; }
What Are You Talking About
解题思路:
结构体里面放字符串,每个节点是存的是火星文,然后火星文的最后一个单词放英文单词。查找的时候如果存在就输出英文单词,如果不存在就原样输出。
代码写的很丑就不放了。
Hat’s Words
标准模板题:
[code]//author: WSM //First Edit Time: 2015-07-29 14:03 //Last Edit Time: 2015-07-29 14:03 #include <map> #include <set> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long using namespace std; const int maxn=26; char str[50000][50]; struct Node { struct Node *next[maxn]; bool flag; }; struct Node *root=new struct Node; void Init(struct Node *proot) { for(int i=0;i<26;i++) { proot->next[i]=NULL; } return; } void Insert(char *s) { struct Node *cur; int len=strlen(s); cur=root; for(int i=0;i<len;i++) { if(cur->next[s[i]-'a']!=NULL) { cur=cur->next[s[i]-'a']; } else { struct Node *newnode=new struct Node; cur->next[s[i]-'a']=newnode; Init(newnode); newnode->flag=false; cur=newnode; } } cur->flag=true; return; } bool judge(char *s) { struct Node *cur; int len=strlen(s); cur=root; for(int i=0;i<len;i++) { if(cur->next[s[i]-'a']!=0) { cur=cur->next[s[i]-'a']; } else return false; } if(cur->flag==true) return true; } int main() { //freopen("test.txt","r",stdin); Init(root); int t=0; while(~scanf("%s",str[t++])) { Insert(str[t-1]); } for(int k=0;k<t;k++) { for(int i=1;i<strlen(str[k]);i++) { char s[50]={'\0'}; char ss[50]={'\0'}; strncpy(s,str[k],i); strncpy(ss,str[k]+i,strlen(str[k])-i); //puts(s); // puts(ss); if(judge(s)&&judge(ss)) { printf("%s\n",str[k]); break; } } } return 0; }
Immediate Decodability
[code]//author: WSM //First Edit Time: 2015-07-29 16:29 //Last Edit Time: 2015-07-29 16:29 #include <map> #include <set> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long using namespace std; struct Node { bool flag; struct Node *next[2]; }; struct Node *root= new struct Node; void Init(struct Node *proot) { for(int i=0;i<2;i++) proot->next[i]=NULL; return; } void Insert(char *str) { struct Node *cur; cur=root; int len=strlen(str); for(int i=0;i<len;i++) { if(cur->next[str[i]-'0']!=NULL) cur=cur->next[str[i]-'0']; else { struct Node *newnode=new struct Node; cur->next[str[i]-'0']=newnode; Init(newnode); newnode->flag=false; cur=newnode; } } cur->flag=true; return; } bool judge(char *str) { int len=strlen(str); struct Node *cur; cur=root; for(int i=0;i<len;i++) { if(cur->next[str[i]-'0']!=NULL) cur=cur->next[str[i]-'0']; else return false; } if(cur->flag==true) return true; else return false; } int main() { char str[1000][1000]; Init(root); int t=0; int flag=0; int mm=1; memset(str,0,sizeof(str)); while(~scanf("%s",str[t++])) { if(!strcmp("9",str[t-1])) { for(int i=0;i<t;i++) { if(strlen(str[i])==1) continue; for(int j=1;j<strlen(str[i]);j++) { char temp[1000]={'\0'}; strncpy(temp,str[i],j); // cout<<temp<<endl; if(judge(temp)) { // cout<<temp<<endl; flag=1; break; } } if(flag) break; } if(!flag) printf("Set %d is immediately decodable\n",mm); else printf("Set %d is not immediately decodable\n",mm); mm++; t=0; flag=0; Init(root); memset(str,0,sizeof(str)); } else Insert(str[t-1]); } return 0; }
相关文章推荐
- G - Strongly connected - hdu 4635(求连通分量)
- leetcode wildcard matching
- getView()执行次数问题(自己再次测试,顺便做个记录)
- 2015年8月1日总结
- 强迫症
- SVN安装与配置
- 解决创建工程时 android.support.v7.app.ActionBarActivity 报错的问题
- 看不惯别人
- android学习之android logat 调试及信息过滤
- 【CODEFORCES】 D. Red-Green Towers
- 在ubuntu kylin 编译cubieboard2 的android镜像碰到的问题
- 关于VS2005智能设备中无法找到PInvoke DLL问题
- 黑马程序员---iOS基础---OC中类创建对象
- struts2学习笔记(五)值栈与ognl
- weiphp的学习
- 常见HTTP状态(304,200等)
- 2015 Multi-University Training Contest 2
- Lucene六(IndexReader的设计)
- Mac下刻录启动盘到u盘
- 通过OpenCV打开摄像头