http://acm.hdu.edu.cn/showproblem.php?pid=2072
题意:中文题目不需要说了!~自己注意一下他输入要求:一行表示一篇文章。
这几天都在刷字典树的题,测试测试自己的模板合适不合适。模板倒是合适的,但是却发现自己在处理数据上容易犯下一些细节错误!~
特在此写下来,谨记自己!~
这份代码是一发就过的:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int MAX=26; 6 const int maxn=1e4+100; 7 int N; 8 9 struct Trie 10 { 11 Trie *next[MAX]; 12 int v;///v要灵活使用,具体情况具体分析 13 }; 14 Trie *root; 15 int Get_ID(char c) 16 { 17 return c-'a'; 18 }///得到字符的id 19 20 void init()///初始化根节点 21 { 22 root=(Trie *)malloc(sizeof(Trie)); 23 for(int i=0; i<MAX; i++) 24 root->next[i]=NULL; 25 root->v=0; 26 } 27 int deal(Trie *p)///删除指针,释放空间,有些题要释放空间不让会MLE 28 { 29 if(p==NULL) 30 return 0; 31 for(int i=0; i<MAX; i++) 32 { 33 if(p->next[i]!=NULL) 34 deal(p->next[i]); 35 } 36 free(p); 37 return 0; 38 } 39 void Insert(char s[])///字典树的建立 40 { 41 Trie *p=root; 42 int len_s=strlen(s); 43 for(int i=0; i<len_s; i++) 44 { 45 int id=Get_ID(s[i]); 46 if(p->next[id]==NULL) 47 { 48 Trie *q=(Trie *)malloc(sizeof(Trie)); 49 for(int j=0; j<MAX; j++) 50 q->next[j]=NULL; 51 q->v=0; 52 p->next[id]=q; 53 } 54 p=p->next[id]; 55 } 56 p->v=1; 57 } 58 int Search(char s[])///查找,看是否有条件满足 59 { 60 Trie *p=root; 61 int len_s=strlen(s); 62 for(int i=0; i<len_s; i++) 63 { 64 int id=Get_ID(s[i]); 65 if(p->next[id]==NULL) 66 return 0;///没有找到以s为前缀的字符串 67 p=p->next[id]; 68 } 69 if(p->v) 70 return 1;///找到了以s串味前缀的字符串 71 return 0; 72 } 73 int main() 74 { 75 char str[maxn]; 76 while(gets(str)) 77 { 78 if(str[0]=='#') 79 break; 80 deal(root); 81 init(); 82 int ans=0; 83 int len=strlen(str); 84 for(int i=0; i<len; i++) 85 { 86 char s[100]; 87 int cnt=0; 88 if(str[i]>='a'&&str[i]<='z') 89 { 90 while(str[i]>='a'&&str[i]<='z') 91 { 92 s[cnt]=str[i]; 93 cnt++; 94 i++; 95 } 96 s[cnt]='\0'; 97 if(!Search(s)) 98 { 99 ans++; 100 Insert(s); 101 } 102 } 103 } 104 printf("%d\n",ans); 105 } 106 return 0; 107 }
下面这份是发现一些细节上的问题,之后改正了,才AC了的。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int MAX=26; 6 const int maxn=1e4+100; 7 int N; 8 9 struct Trie 10 { 11 Trie *next[MAX]; 12 int v;///v要灵活使用,具体情况具体分析 13 }; 14 Trie *root; 15 int Get_ID(char c) 16 { 17 return c-'a'; 18 }///得到字符的id 19 20 void init()///初始化根节点 21 { 22 root=(Trie *)malloc(sizeof(Trie)); 23 for(int i=0; i<MAX; i++) 24 root->next[i]=NULL; 25 root->v=0; 26 } 27 int deal(Trie *p)///删除指针,释放空间,有些题要释放空间不让会MLE 28 { 29 if(p==NULL) 30 return 0; 31 for(int i=0; i<MAX; i++) 32 { 33 if(p->next[i]!=NULL) 34 deal(p->next[i]); 35 } 36 free(p); 37 return 0; 38 } 39 void Insert(char s[])///字典树的建立 40 { 41 Trie *p=root; 42 int len_s=strlen(s); 43 for(int i=0; i<len_s; i++) 44 { 45 int id=Get_ID(s[i]); 46 if(p->next[id]==NULL) 47 { 48 Trie *q=(Trie *)malloc(sizeof(Trie)); 49 for(int j=0; j<MAX; j++) 50 q->next[j]=NULL; 51 q->v=0; 52 p->next[id]=q; 53 } 54 p=p->next[id]; 55 56 } 57 p->v=1; 58 } 59 int Search(char s[])///查找,看是否有条件满足 60 { 61 Trie *p=root; 62 int len_s=strlen(s); 63 for(int i=0; i<len_s; i++) 64 { 65 int id=Get_ID(s[i]); 66 if(p->next[id]==NULL) 67 return 0;///没有找到以s为前缀的字符串 68 p=p->next[id]; 69 } 70 if(p->v) 71 return 1;///找到了以s串味前缀的字符串 72 return 0; 73 } 74 int main() 75 { 76 77 char str[maxn]; 78 init(); 79 while(gets(str)) 80 { 81 if(str[0]=='#') 82 break; 83 deal(root); 84 init(); 85 int ans=0; 86 int len=strlen(str); 87 for(int i=0; i<len; i++) 88 { 89 char s[100]; 90 int cnt=0; 91 while(str[i]>='a'&&str[i]<='z') 92 { 93 s[cnt]=str[i]; 94 cnt++; 95 i++; 96 } 97 s[cnt]='\0'; 98 if(cnt&&!Search(s))///这里要判断cnt是否为0, 99 { ///因为如果为0.我们肯定查找不到,从而使答案多加1了 100 ans++; ///算是一个小细节吧! 101 Insert(s); 102 } 103 } 104 printf("%d\n",ans); 105 } 106 return 0; 107 }
怎么感觉自己每次都不能一次AC,每次都会在细节上出点错误!~好心痛啊,哎~仔细啊~仔细~