hdu1075
来源:互联网 发布:apache 虚拟目录 编辑:程序博客网 时间:2024/05/16 02:37
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075
基本上是个字典树的裸题了,不过数据处理的比较麻烦,纠结了好久。。。
#include<iostream>#include<cstdio>#include<cstring>#include<string>using namespace std;class Trie{public: Trie(){} void init() { for(int i = 0;i < 500000;++i) { tr[i].pos = 0; tr[i].str.clear(); memset(tr[i].next,NULL,sizeof(tr[i].next)); } head = &tr[0]; pos = 0; } void insert(string st,string st1) { struct tree *h = head; for(int i = 0;i < st1.size();++i) { if(h->next[st1[i] - 'a'] != NULL) h = h->next[st1[i] - 'a']; else { h->next[st1[i] - 'a'] = &tr[++pos]; h = h->next[st1[i] - 'a']; } } h->str = st; h->pos = 1; } void search(string st) { struct tree *h = head; bool flag = false; for(int i = 0;i <st.size();++i) { if(h->next[st[i] - 'a'] !=NULL) h = h->next[st[i] - 'a']; else {h = NULL;break;} } if(h && h->pos) cout<<h->str; else cout<<st; } ~Trie(){}private: struct tree { int pos; string str; struct tree *next[30]; }tr[500000]; struct tree *head; int pos;};class Trie t;int main(){ //freopen("1.txt","r",stdin); char st1[3005],st2[3005],begin[10] = "START",end[10] = "END"; t.init(); scanf("%s",st1); while(1) { scanf("%s",st1); if(strcmp(st1,end) == 0) break; scanf("%s",st2); t.insert(st1,st2); } scanf("%s",st1);getchar(); while(1) { gets(st1); if(strlen(st1) == 3 && strcmp(st1,end) == 0) break; int j = 0; memset(st2,'\0',sizeof(st2)); for(int i = 0;i < strlen(st1);++i) { if(st1[i] <= 'z' && st1[i] >= 'a') { st2[j++] = st1[i]; } else { st2[j] = '\0'; t.search(st2); printf("%c",st1[i]); j = 0; memset(st2,'\0',sizeof(st2)); } } if(strlen(st2) != 0) { t.search(st2); } printf("\n"); } return 0;}
- hdu1075
- HDU1075
- hdu1075
- hdu1075
- hdu1075
- hdu1075
- hdu1075
- HDU1075
- hdu1075
- hdu1075
- hdu1075
- trie hdu1075
- hdu1075 map
- hdu1075【map】
- hdu1075 map
- hdu1075--还是字典树
- HDU1075 字典树
- hdu1075之字典树
- excel导出时 如何根据java后台返回的结果 动态实现excel复选框的勾选
- oracle笔记
- SQLite 入门教程(四)增删改查操作详解
- undefined reference问题总结--转载
- 解决Ubuntu 10.04上网速度慢的问题
- hdu1075
- android - Jni 接口函数表
- oracle創建自增長的鍵
- Vmware下安装ubuntu如何上Google Doc
- ARM Instructions
- 让程序暂停的几种方法
- ubuntu系统下如何查看当前系统的IP地址
- MyEclipse性能优化
- c++最大最小值