字典树学习
来源:互联网 发布:淘宝达人如何申请 编辑:程序博客网 时间:2024/05/17 01:57
题目描述:假设有很多个单词输入。输出最后出现次数最多的那个。
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>using namespace std ;struct DicTree{ struct DicTree * next[26] ; int cnt ;}*a;void init(){ a = new DicTree ; for(int i = 0 ;i<26 ;i++) { a->next[i] = NULL ; }}int insert(char str[]){ int len = strlen(str) ; DicTree * head = a ; for(int i = 0 ;i<len ;i++) { int res = (int)(str[i]-97) ; if(head->next[res] == NULL) { head->next[res] = new DicTree ; head = head->next[res] ; head->cnt = 0 ; for(int j = 0 ;j<26 ;j++) { head->next[i] = NULL ; } } else { head = head->next[res] ; } } head->cnt++ ; return head->cnt ;}int main(){ int num ; int maxNum ; int tmp ; char str[100],ans[100] ; scanf("%d",&num) ; init() ; maxNum = 0 ; for(int i = 0 ;i<num ;i++) { scanf("%s",str) ; tmp = insert(str) ; if(tmp > maxNum) { maxNum = tmp ; strcpy(ans,str) ; } } printf("the max num is %d the word is %s\n",maxNum,ans) ; return 0 ;}
0 0
- 字典树学习
- 字典树学习材料
- 字典树学习
- 学习 字典树
- 字典树学习
- 字典树学习
- 字典树学习小结
- 字典树学习总结
- 字典树 学习笔记
- 【学习笔记】关于字典树
- Trie(字典树)的学习
- 字典树(基础学习)
- 字典树学习与使用
- 字典学习
- 字典学习
- 树学习 ---------字典树(Trie Tree)
- 【学习记录】trie树(字典树)
- 字典树的痛苦学习经历
- 百度如果不做医疗行业的推广,你搜索到的就都是真的了?
- C语言中的__attribute__机制
- swift 使用UIButton
- popWindow与EditText和软键盘的相爱相杀
- android studio 小白使用记
- 字典树学习
- “Pushing the same view controller instance more than once is not supported” exception
- Qt之JSON生成与解析
- Android控件CheckBox和RadioButton
- Oracle Crystal ball 11.1.* 注册码
- FPGA异步时钟设计中的同步策略
- Cocos2d-JS 粒子效果
- python log模块使用实例
- SQLite + FMDB 性能笔记