HDU 1075 1247(字典树)
2014-04-28 14:26
363 查看
1075
1247
/* 字典树 hdu 1075 火星文翻译 */ #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int MAX = 26; struct node { int count ;//记录到此是否是某个火星词的结尾 char s[15];//如果是某个火星词的结尾则存储对应的英文单词 struct node *next[MAX]; }; struct node *root; struct node *build() { node *p; p=(struct node *)malloc(sizeof(struct node)); for(int i = 0; i < 26; i++) { p->next[i] = NULL; } p->count = -1; return p; } void Insert(char *ch,char *s) { int len=strlen(s); if(len==0)return ; node *p; p = root; for(int i = 0; i < len; i++) { if(p->next[s[i]-'a']!=NULL) { p=p->next[s[i]-'a']; } else { p->next[s[i]-'a']=build(); p=p->next[s[i]-'a']; } } p->count = 1; strcpy(p->s,ch); } int Query(char *s) { struct node *p; int len = strlen(s); if(len==0)return 0; p = root; for(int i=0;i<len ;i++) { if(p->next[s[i]-'a']!=NULL) p=p->next[s[i]-'a']; else return 0; } if(p->count==1) { printf("%s",p->s); return 1; } return 0;//这个地方要注意,该单词有可能只是某个词的前缀 } int main() { char str[15],str1[15],str2[15],str3[3005]; root = build(); gets(str); while(scanf("%s",str1) && strcmp(str1,"END")!=0) { getchar(); scanf("%s",str2); getchar(); Insert(str1,str2); } getchar(); gets(str2); gets(str3); while(strcmp(str3,"END")!=0) { int len = strlen(str3); for(int i = 0; i < len;) { if(str3[i] < 'a' || str3[i] > 'z') { printf("%c",str3[i]); i++; } else { int t = 0; while(str3[i]>='a'&&str3[i]<='z') { str1[t++] = str3[i]; i++; } str1[t] = '\0'; if(Query(str1)==0)printf("%s",str1); } } printf("\n"); gets(str3); } return 0; }
1247
/* 字典树 hdu 1247 判断某个单词是不是由给出的单词表中的任意两个单词组合成的 */ #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <stack> #include <algorithm> using namespace std; const int MAX = 26; struct node { int count ;//记录到此是否是某个词的结尾 struct node *next[MAX]; }; struct node *root; struct node *build() { node *p; p=(struct node *)malloc(sizeof(struct node)); for(int i = 0; i < 26; i++) { p->next[i] = NULL; } p->count = 0; return p; } void Insert(char *s) { int len=strlen(s); if(len==0)return ; node *p; p = root; for(int i = 0; i < len; i++) { if(p->next[s[i]-'a']!=NULL) { p=p->next[s[i]-'a']; } else { p->next[s[i]-'a']=build(); p = p->next[s[i]-'a']; } } p->count = 1; } int Query(char *s) { int len = strlen(s); node *p; p = root; stack<int > k; int i = 0; while(i < len) { if(p->next[s[i]-'a']==NULL)return 0; p = p->next[s[i]-'a']; if(p->count==1) //找到该单词含有子单词的分隔点 { k.push(i); } //入栈 i++; } while(!k.empty()) { int i = k.top() + 1; k.pop(); node *p = root; bool flag = true; while(i < len) { if(p->next[s[i]-'a']==NULL) { flag = false; break; } p = p->next[s[i]-'a']; i++; } if(flag && p->count==1)//找到最后,并且是单词的结尾 return 1; } return 0; } //void Release(node *p) //{ // if(p==NULL) // return ; // for(int i = 0; i < MAX; i++) // { // if(p->next[i]!=NULL) // Release(p->next[i]); // } // free(p); // return ; //} int main() { char m[50005][20]; int i = 0; root = build(); while(~scanf("%s",m[i])) { Insert(m[i++]); } for(int j = 0; j < i; j++) { if(Query(m[j])==1) puts(m[j]); } return 0; }
相关文章推荐
- 必须弄懂的495个C语言问题
- sqlserver常用日期、时间函数和格式
- 修改easyui的easyloader的默认css目录路径
- 帧中继下的OSPF的网络类型
- java异常【Exception】
- C++ 字符串操作经验集
- Median of Two Sorted Arrays
- PAT1012 BASIC:数字分类 (20)
- 算法基础——1.2枚举与剪枝(找零钱、n位数平方的尾数还是自己)
- SQL Server Configuration Manager
- 怎么忽悠才能让创业项目听上去很靠谱?
- ASP.NET MVC 4 (三) 过滤器
- WebService之WSDL
- loadrunner 发送gzip压缩json格式
- 不重新编译php添加扩展模块
- Error inflating class android.support.v4.view.ViewPaper
- 从一道面试题彻底搞懂hashCode与equals的作用与区别及应当注意的细节
- HDU 1251 1671 (简单字典树)
- oracle函数instr和decode、nvl
- vector 反向迭代器的使用