HDU 1075 What Are You Talking About(Tire树)
2012-08-13 20:36
323 查看
题目链接
写的太渣了,依旧RE了几次。。。
写的太渣了,依旧RE了几次。。。
#include <stdio.h> #include <string.h> #include <stdlib.h> char str[500001][101]; char word[500001]; char ch[5001]; struct node { int flag; struct node *next[26]; }; int num = 1; struct node *build() { int i; struct node *p; p = (struct node *)malloc(sizeof(struct node)); for(i = 0;i <= 25;i ++) p -> next[i] = NULL; return p; } void insert(struct node *head,char *str1) { int i; struct node *p; p = head; for(i = 0;str1[i];i ++) { if(p -> next[str1[i]-'a'] == NULL) { p -> next[str1[i]-'a'] = build(); } p = p -> next[str1[i] - 'a']; } p -> flag = num; } int find(struct node *head,char *s) { int i,j,len; struct node *p; p = head; len = strlen(s); for(i = 0;i <= len;i ++) { if(s[i] >= 'a'&&s[i] <= 'z') { if(p -> next[s[i]-'a'] == NULL) { for(j = 0;j <= i;j ++) { printf("%c",s[j]); } return i; } else p = p -> next[s[i]-'a']; } else { if(p -> flag > 0&&p ->flag <= num-1) { printf("%s",str[p->flag]); return i-1; } else { for(j = 0;j <= i-1;j ++) { printf("%c",s[j]); } return i-1; } } } return i-1; } void search(struct node *head,char *str1) { int i,len; len = strlen(str1); for(i = 0;i <= len-1;i ++) { if(str1[i] <= 'z'&&str1[i] >= 'a') { i += find(head,str1+i); } else printf("%c",str1[i]); } printf("\n"); } int main() { struct node *head; head = build(); for(;;) { scanf("%s%*c",ch); if(strcmp("END",ch) == 0) break; if(strcmp("START",ch)!= 0) { scanf("%s%*c",word); strcpy(str[num],ch); insert(head,word); num ++; } } for(;;) { gets(word); if(strcmp("END",word) == 0) break; if(strcmp("START",word)!= 0) { search(head,word); } } return 0; }
相关文章推荐
- HDU-1075 What Are You Talking About (STL:map)
- 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(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
- HDU ACM 1075 What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About
- HDU 1075.What Are You Talking About【Map水题(字典树)】【5月25】
- hdu 1075 What Are You Talking About
- hdu 1075 What Are You Talking About
- HDU——T 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的使用