What Are You Talking About(hdu1075字典树)
来源:互联网 发布:数据线上带充电宝 编辑:程序博客网 时间:2024/06/06 08:49
题意:给你每个火星文的翻译,再给你一句话然后将他翻译过来,标点不算,如果没找到就按照原文翻译
思路:字典树,每读一个单词检查字典树看是否存在,如果存在就将译文输出,标点照常输出,不存在就原文输出
1.判断单词是否存在,需要标记每个单词是否完结,否则会出现前缀
2.用结构体保存两个字符串,译文在查找串的末尾节点整体保存。方便查找,一旦找到直接输出
代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;struct Tree{ bool flag; char str[15]; struct Tree* child[26];}Tri[1000000];int alloc = 0;struct Tree*creat_node(){ Tree * p; p = & Tri[alloc++]; int i; for(i = 0; i < 26; i++) { p -> child[i] = NULL; } p -> flag = false; memset(p->str,'\0',sizeof(p->str)); return p;}void insert(Tree **p,char *str,char *cstr){ int i,j,len; len = strlen(str); Tree * q; q = *p; for(i = 0; i < len; i++) { if(q -> child[str[i]-'a'] != NULL) { q = q -> child[str[i]-'a']; } else { q ->child[str[i]-'a'] = creat_node(); q = q ->child[str[i]-'a']; } } q -> flag = true; strcpy(q->str,cstr);}struct Tree *find(Tree**p,char *str){ int i,len; len = strlen(str); Tree * q; q = *p; for(i =0; i < len; i++) { if(q -> child[str[i]-'a'] != NULL) { q = q ->child[str[i]-'a']; } else { return NULL; } } if(q -> flag) return q; else return NULL;}int main(){ char c[10]; while(scanf("%s",c) != EOF) { Tree * root; root = creat_node(); if(strcmp(c,"START") == 0) { char str1[20];char str2[20]; while(scanf("%s",str1) != EOF) { if(strcmp(str1,"END") == 0) break; else { scanf("%s",str2); insert(&root,str2,str1); } } } char c[5000]; char ccc[20]; char cc[20]; scanf("%s",ccc); if(strcmp(ccc,"START") == 0) { getchar(); while(gets(c) != NULL) { int len = strlen(c); if(strcmp(c,"END") == 0) return 0; int i,j = 0; c[len] = ' ';c[len+1] = '\0'; for(i = 0; i <= len; i++) { if('a' <= c[i] && c[i] <= 'z') { cc[j] = c[i];j++; } else { Tree * t = NULL ;cc[j] = '\0'; t = find(&root,cc); if(t == NULL) { printf("%s",cc); } else { printf("%s",t->str); } if(i != len) printf("%c",c[i]);j = 0; memset(cc,'\0',sizeof(cc)); } } printf("\n"); } } } return 0;}
- hdu1075 What Are You Talking About-字典树
- 【字典树】 hdu1075 What Are You Talking About
- HDU1075 What Are You Talking About 解题报告--字典树
- What Are You Talking About(hdu1075字典树)
- Hdu1075 - What Are You Talking About - 字典树
- hdu1075 What Are You Talking About (字典树)
- hdu1075-What Are You Talking About 字典树
- What are you talking about(HDU1075)
- What Are You Talking About hdu1075
- hdu1075 What Are You Talking About
- hdu1075-What Are You Talking About
- HDU1075:What Are You Talking About
- HDU1075 What Are You Talking About
- hdu1075 What Are You Talking About
- HDU1075 What Are You Talking About 【STL】
- What Are You Talking About hdu1075
- hdu1075 What Are You Talking About
- HDU1075 What Are You Talking About
- uva 10557
- C++通过写注册表实现将文件夹映射为系统虚拟盘
- IOS 图片阴影,圆角等处理
- 解决sqlplus连接oracle乱码
- Delphi SendMessage 使用
- What Are You Talking About(hdu1075字典树)
- 如何查看linux/unix查看wwn号
- 第一次进csdn的博客
- Win8 安装Oracle 10g 提示“程序异常终止”的解决方案
- 开启远程数据库组件
- Iphone控件大全
- hdu -- 1443 Joseph
- IE6 链接无法点击的修复方法
- 【转载】不刷机,I9300通过修改资源文件加入通话录音功能