博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 10391字典树
阅读量:4621 次
发布时间:2019-06-09

本文共 2038 字,大约阅读时间需要 6 分钟。

View Code
1 #include 
2 #include
3 4 const int sonnum = 26; 5 char word[120005][30]; 6 struct trie 7 {
8 bool term; 9 struct trie *son[sonnum]; 10 }; 11 12 trie* create_trie() 13 {
14 trie *temp = new trie; 15 temp -> term = false; 16 for(int i = 0;i
son[i] =NULL; 19 } 20 21 return temp; 22 } 23 24 void insert_word(trie *pnt,char s[],int len) 25 {
26 trie *temp = pnt; 27 for(int i = 0;i
son[s[i] - 'a'] == NULL) 30 temp -> son[s[i] - 'a'] = create_trie(); 31 temp = temp -> son[s[i] - 'a']; 32 } 33 temp -> term = true; 34 } 35 36 bool find(trie *pnt ,char s[],int len) 37 {
38 trie *temp = pnt; 39 for(int i = 0;i
son[s[i] - 'a'] == NULL) 42 return false; 43 temp = temp -> son[s[i] - 'a']; 44 } 45 46 if(temp -> term) 47 return true; 48 return false; 49 } 50 51 int main() 52 {
53 int n = 0; 54 trie *t = create_trie(); 55 while(scanf("%s",word[n]) == 1) 56 {
57 insert_word(t,word[n],strlen(word[n])); 58 n ++; 59 } 60 61 for(int i = 0;i < n;i ++) 62 {
63 char s1[30],s2[30]; 64 int len = strlen(word[i]); 65 for(int j = 1;j < len ;j ++) 66 {
67 int k; 68 for(k = 0;k < j;k ++) 69 {
70 s1[k] = word[i][k]; 71 } 72 s1[k] = '\0'; 73 74 for(k = j;k < len;k ++) 75 {
76 s2[k-j] = word[i][k]; 77 } 78 s2[k-j] = '\0'; 79 if(find(t,s1,strlen(s1))&&find(t,s2,strlen(s2))) 80 {
81 printf("%s\n",word[i]); 82 break; 83 } 84 } 85 } 86 return 0; 87 }

知道这一题可以用hash来做,但是我不会……~~~~(>_<)~~~~

转载于:https://www.cnblogs.com/Shirlies/archive/2012/03/15/2398107.html

你可能感兴趣的文章
maven中使用tomcat进行热部署
查看>>
Bootstrap学习第二天轮播插件
查看>>
LeetCode -- Increasing Triplet Subsequence
查看>>
hdu 2114 Calculate S(n) 数论(简单题)
查看>>
hdu 5984
查看>>
解决:Incorrect line ending: found carriage return (\r) without corresponding newline (\n)
查看>>
深入学习微框架:Spring Boot
查看>>
Coprimes - SGU 102(求互质数,水)
查看>>
CSS布局(二) 盒子模型属性
查看>>
jQuery 获取select选中的option
查看>>
更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支
查看>>
LeetCode 112. Path Sum (二叉树路径之和)
查看>>
mysql数据恢复
查看>>
java list
查看>>
算法练习2---斐波那契数列java版
查看>>
用VISIO2013绘制E-R图
查看>>
每日站立会议03
查看>>
软件工程第一次作业
查看>>
初步了解HTML
查看>>
九度OJ 1165:字符串匹配 (模式匹配)
查看>>