HDU ACM 1075 What Are You Talking About(字典树)
2013-04-07 00:38
531 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1075
#include <iostream> #include <string> using namespace std; struct Node { char str[20]; int flag; Node *child[26]; }; void NewNode(Node **Q)//注意这里使用了指针的指针 { (*Q) = (Node *)malloc(sizeof(Node)); int i; for(i=0;i<26;i++) { (*Q)->child[i] = NULL; } } void Insert(Node **T,char str[],char set[])//注意这里使用了指针的指针 { Node *Q = *T; int len = strlen(str); int i; for(i=0;i<len;i++) { if(Q->child[str[i] - 'a'] != NULL) { Q = Q->child[str[i] - 'a']; } else { NewNode(&(Q->child[str[i] - 'a'])); Q = Q->child[str[i] - 'a']; } } Q->flag = 1; strcpy(Q->str,set); } void Search(Node *Q,char str[]) { int len = strlen(str); if(len == 0) { return ; } int i; for(i=0;i<len;i++) { if(Q->child[str[i] - 'a'] != NULL) { Q = Q->child[str[i] - 'a']; } else { break; } } if(Q->flag == 1 && i == len) { cout<<Q->str; } else { cout<<str; } } int main() { char str[3100]; Node *T = NULL; NewNode(&T); int flag = 0; cin>>str; getchar(); while(1) { gets(str); if( strcmp(str,"END") == 0 ) { break; } else { int i,j; char english[20] = {0}; char mars[20] = {0}; for(i=0;1;i++) { if(str[i] >= 'a' && str[i]<='z') { english[i] = str[i]; } else { english[i] = 0; break; } } for(i=i+1,j=0;1;i++,j++) { if(str[i] >= 'a' && str[i]<='z') { mars[j] = str[i]; } else { mars[j] = 0; break; } } Insert(&T,mars,english); } } cin>>str; getchar(); while(1) { gets(str); if( strcmp(str,"END") == 0 ) { break; } else { int len = strlen(str); char mid[20] = {0}; int i,j; for(i=0,j=0;i<len;i++) { if(str[i] >= 'a' && str[i]<='z') { mid[j] = str[i]; j++; } else { mid[j] = 0; j=0; Search(T,mid); cout<<str[i]; } } if(mid[0] != 0) { mid[j] = 0; Search(T,mid); } cout<<endl; } } return 0; } /* START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh */
相关文章推荐
- 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【字典树 & STL】
- hdu 1075 What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About (map 或者 字典树)
- 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 字典树 或 stl map
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
- hdu acm 1075 What Are You Talking About
- HDU 1075 What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About (STL, 字典树)
- hdu 1075-What Are You Talking About(Map&&字典树)
- hdu 1075 What Are You Talking About(HashMap水过,可用字典树)
- HDU 1075 What Are You Talking About(字典树)
- 字典树详解(附题:HDU 1075 What are you talking about)
- HDU_1075_What Are You Talking About(字典树)