What Are You Talking About hdu1075
2012-10-20 14:07
399 查看
这题的字符串处理真纠结,搞得我都想吐了,晕,其它的好像没什么好说的,就是个简单的字典树了。
不过通过做这道题,我对scanf和gets的理解更加透彻了,scanf读取字符串时,是从第一的非空格的字母开始读,再次遇到空格或'\n'时结束,它不会处理掉'\r';而gets则按行读(连空格也一起读),遇到'\r'时将其转换为'\0'然后结束。
不过通过做这道题,我对scanf和gets的理解更加透彻了,scanf读取字符串时,是从第一的非空格的字母开始读,再次遇到空格或'\n'时结束,它不会处理掉'\r';而gets则按行读(连空格也一起读),遇到'\r'时将其转换为'\0'然后结束。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; struct node { char word[11]; bool isTail; node *next[26]; }memory[1000000],*root; int cur; node *build_node() { node *p=&memory[cur++]; for(int i=0;i<26;i++) p->next[i]=NULL; p->isTail=false; return p; } void insert_tree(char *s1,char *s2) { int len=strlen(s1); node *p=root; for(int i=0;i<len;i++) { if(p->next[s1[i]-'a']==NULL) p->next[s1[i]-'a']=build_node(); p=p->next[s1[i]-'a']; } p->isTail=true; strcpy(p->word,s2); } char *query(char *s) { int len=strlen(s); node *p=root; for(int i=0;i<len;i++) { if(p->next[s[i]-'a']==NULL) return NULL; p=p->next[s[i]-'a']; } if(p->isTail!=NULL) return p->word; else return NULL; } int main() { char s1[15],s2[15],s[3005],*p,*q; int i; bool state; cur=0; root=build_node(); gets(s1);//读"START" //建字典树 while(scanf("%s",s1)) { if(s1[0]=='E') break; scanf("%s",s2); insert_tree(s2,s1); } //翻译 getchar();//处理最后一个‘\n'。 gets(s2);//读"START" while(gets(s)) { if(s[0]=='E') break; p=s; i=0; while(*p) { if(*p<='z'&&*p>='a') { state=true;//在单词中 s1[i++]=*p; } else if(state==true) { state=false;//出了单词 s1[i]='\0'; i=0; if((q=query(s1))==NULL) printf("%s",s1); else printf("%s",q); putchar(*p); } else putchar(*p); p++; } putchar('\n'); } return 0; }
相关文章推荐
- 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 1075 What Are You Talking About 【map + find】
- 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(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 1075 What Are You Talking About(字典树模板题)
- hdu 1075 What Are You Talking About STL的使用
- HDU 1075 What Are You Talking About (字典树)