hdu 1075 What Are You Talking About (字典树)
来源:互联网 发布:群晖 路由器端口转发 编辑:程序博客网 时间:2024/05/23 19:15
初学字典树
此题涉及建立Trie tree和查找操作,只是输入有些复杂。
#include<stdio.h>#include<string.h>#include<stdlib.h>struct node{ struct node *child[26]; char *str;}*root;void insert(char *c1,char *c2){ int len=strlen(c2); struct node *cur,*newnode; cur=root; for(int i=0;i<len;i++) { if(cur->child[c2[i]-'a']!=0)//如果此处与前字符串有相同的关键字,那么跳到此关键字的孩子节点继续操作 cur=cur->child[c2[i]-'a']; else { newnode=new struct node; cur->child[c2[i]-'a']=newnode; for(int j=0;j<26;j++) newnode->child[j]=0; newnode->str=NULL; cur=newnode; } } cur->str=new char[15];//为一个字符串建立对应位置后,将对应的译文放入str中。 strcpy(cur->str,c1);}void find_print(char *c2){ int len=strlen(c2); struct node *cur; cur=root; if(!len) return ; for(int i=0;i<len;i++) { if(c2[i]<'a'||c2[i]>'z'||cur->child[c2[i]-'a']==0) { printf("%s",c2); return ; } else cur=cur->child[c2[i]-'a']; } if(cur->str!=NULL)//如果找到了,且相应节点上存放着字符串,则输出,即所查找的单词不是前缀 printf("%s",cur->str); else printf("%s",c2); }int main(){ char s[15],temp[3005],c1[15],c2[15]; root=new struct node; for(int i=0;i<26;i++) root->child[i]=0; scanf("%s",temp); while(scanf("%s",c1)&&strcmp(c1,"END")!=0) { scanf("%s",c2); insert(c1,c2); } scanf("%s",temp); getchar(); while(gets(temp)&&strcmp(temp,"END")!=0) { int len=strlen(temp); int num=0; bool flag=false; for(int i=0;i<len;i++) { if(temp[i]>='a'&&temp[i]<='z') { if(flag==false) flag=true; s[num++]=temp[i]; } else { if(flag) { flag=false; s[num]='\0'; num=0; find_print(s); } printf("%c",temp[i]); } } if(flag) { s[num]='\0'; find_print(s); } printf("\n"); } return 0;}
- HDU 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About (字典树)
- hdu-1075-What Are You Talking About-(字典树)
- hdu 1075 What Are You Talking About(字典树方法)
- hdu 1075 What Are You Talking About 字典树
- HDU 1075 What Are You Talking About 字典树
- hdu 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About 字典树
- hdu 1075 What Are You Talking About(字典树)
- [字典树] HDU 1075 - What Are You Talking About
- hdu 1075 What Are You Talking About 字典树 trie
- hdu 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About 字典树
- HDU 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About(字典树)
- 【字典树】hdu 1075 What Are You Talking About
- 建立时间与保持时间(两个寄存器的详细分析)(转)
- GetPrivateProfileString
- 11g新特性之–Query Cache Result 研究
- MyBatis学习 之 五、MyBatis配置文件
- 利用Aspose.Words,在word文档中创建表格
- hdu 1075 What Are You Talking About (字典树)
- JAVA分页总结
- OFBIz之旅[结构解析]
- Linux点滴-platform_set_drvdata 和 platform_get_drvdata
- Android 导入多个第三方jar包
- selenium 读取xml
- 匹配 数字 或者 数字,数字 ...的正则表达式
- 西班牙报纸为何大骂中国人是“猪”
- is not allowed to connect to this MySQL server