Hdu 1075+ Hdu 1251 (动态Trie)
来源:互联网 发布:mac口红chili专柜价格 编辑:程序博客网 时间:2024/04/28 08:01
大概两周前写的,拿出来总结下。
貌似这两题都没告诉数据规模,所以才想起练练动态写法,真心不喜欢用指针……
Hdu 1075
//重新写了一个动态开辟节点的.本题用map应该也可以#include <cstdio>#include <cstring>#include <iostream>using namespace std;struct Trie{Trie *next[26];char dic[10];Trie (){dic[0]=0;for (int i=0;i<26;i++)next[i]=NULL;}};Trie *root=NULL;char str1[15],str2[15];void Insert (){Trie *p=root;if (p==NULL){root=new Trie();p=root;}int i=0;while (str2[i]){if (p->next[str2[i]-'a']==NULL)p->next[str2[i]-'a']=new Trie();p=p->next[str2[i++]-'a'];}strcpy (p->dic,str1);}bool Find (){Trie *p=root;if (p==NULL)return false;int i=0;while (str1[i]){if (p->next[str1[i]-'a']==NULL)return false;p=p->next[str1[i++]-'a'];}if (p->dic[0]==0)return false;printf("%s",p->dic);return true;}int main (){#ifdef ONLINE_JUDGE#elsefreopen("read.txt","r",stdin);#endifscanf("%*s");while (scanf("%s",str1),strcmp(str1,"END")){scanf("%s",str2);Insert();}scanf("%*s");int i=0;bool flag=false; //标记是否读入了一个单词char ch;getchar();while (scanf("%c",&ch))if (ch>='a'&& ch<='z'){str1[i++]=ch;flag=true;}else{if (flag){str1[i]=0;if (Find()==false) //找到时在Find()中输出printf("%s",str1);flag=false;}i=0;if (ch=='E')break;printf("%c",ch);}return 0;}
Hdu 1251
#include <cstdio>#include <cstring>#include <iostream>using namespace std;struct Trie{int num;Trie *next[26];Trie (){num=0;for (int i=0;i<26;i++)next[i]=NULL;}};Trie *root=NULL;void Insert (char str[]){Trie *p=root;if (p==NULL){root=new Trie();p=root;}int i=0;while (str[i]){if (p->next[str[i]-'a']==NULL)p->next[str[i]-'a']=new Trie();p=p->next[str[i++]-'a'];p->num++;}}int Find (char str[]){Trie *p=root;if (p==NULL)return false;int i=0;while (str[i]){if (p->next[str[i]-'a']==NULL)return 0;p=p->next[str[i++]-'a'];}return p->num;}int main (){#ifdef ONLINE_JUDGE#elsefreopen("read.txt","r",stdin);#endifchar str[15];while (gets(str),strcmp(str,""))Insert (str);while (~scanf("%s",str))printf("%d\n",Find (str));return 0;}
- Hdu 1075+ Hdu 1251 (动态Trie)
- hdu 1075 Trie树
- HDU 1251 Trie树
- hdu 1251 [Trie]
- hdu 1251 trie
- hdu 1251 trie
- hdu 1251(trie)
- hdu 1251 trie树
- HDU 1251 Trie入门
- hdu 1251统计难题(trie)
- hdu 1251 统计难题 Trie
- Trie树入门:HDU 1251
- 【Trie模板更新】【hdu 1251】
- hdu 1251 统计难题(trie)
- HDU 1251 Trie字典树
- hdu 1251 统计难题 trie
- hdu 1251 入门trie树
- HDU-1251 统计难题 (Trie)
- 九宫格遍历(dfs)深搜图示
- 第五次上机
- 【昊昊带你学】深搜(DFS)
- 【30篇突击 android】源码统计 十七
- 析构函数不能失败的理由
- Hdu 1075+ Hdu 1251 (动态Trie)
- andriod开发之自动开关机实现代码
- Ubuntu 12.10安装后需要做的那些事
- 线段树
- Android之TelephonyManager类的方法详解
- 【昊昊带你学】软院特别篇-简单订票系统
- twoSum
- Google Map Android v2开发: 安装运行Google Map Android v2 示例程序
- 改良程序的11技巧