HDU-2846Repository(字典树)
来源:互联网 发布:淘宝到货短信 编辑:程序博客网 时间:2024/04/27 14:07
TLE状态
#include <iostream>#include <string>#include <fstream>#include <set>class dr_tree{private:typedef struct node{bool flag;node* next[26];node(){flag=false;for(int i=0;i!=26;i++){next[i]=NULL;}}} node_t;public:dr_tree(){ root_=new node_t(); }bool insert(std::string& str){node_t* n_tmp=root_;int idx;int size_=str.size();for(int i=0;i!=size_;i++){idx=str[i]-'a';if(n_tmp->next[idx]==NULL){n_tmp->next[idx]=new node_t();}n_tmp=n_tmp->next[idx];}n_tmp->flag=true;return true;}int query(std::string& str){int num(0);node_t* n_tmp1=root_;node_t* n_tmp2=query(n_tmp1,str,num);query_set_.clear();return num;}~dr_tree(){}private:node_t* query(node_t* n,std::string &str,int& num){if(n==NULL){return NULL;}bool flag=false;node_t* tmp;int num_tmp(0);for(int i=0;i!=26;i++){if((n->next[i]!=NULL)&&(i+'a'==str[0])){if((tmp=test_str(n->next[i],str))!=NULL){query(tmp,num_tmp); num+=num_tmp;}}query(n->next[i],str,num);}return NULL;}node_t* test_str(node_t* n,std::string & str){int idx;for(int i=1;i!=str.size();i++){idx=str[i]-'a';if(n->next[idx]!=NULL){n=n->next[idx];}else{return NULL;}}return n;}int query(node_t* n,int & num){if(n==NULL){return 0;}if(n->flag==true){ if(query_set_.insert(n).second){ num++;}} for(int i=0;i!=26;i++){query(n->next[i],num);}}node_t* root_;std::set<node_t*> query_set_;};int p,q;std::string str;dr_tree tree;int main(){ //std::fstream std::cin("1.txt");std::cin>>p;for(int i=0;i!=p;i++){std::cin>>str;tree.insert(str);}std::cin>>q;for(int i=0;i!=q;i++){std::cin>>str;std::cout<<tree.query(str)<<std::endl;}}
- HDU-2846Repository(字典树)
- HDU 2846 Repository(字典树变形)
- HDU 2846 Repository(字典树)
- HDU - 2846 - Repository(字典树)
- HDU-2846-Repository(字典树)
- hdu 2846 Repository(字典树)
- hdu-2846-Repository(字典树)
- HDU 2846Repository(字典树+计数)
- 【字典树】hdu 2846 Repository
- hdu 2846 Repository (字典树)
- HDU--2846--Repository--字典树
- hdu 2846 Repository (字典树)
- hdu 2846 Repository 字典树
- HDU 2846 Repository (字典树)
- hdu Repository(字典树)
- HDU 2846 Repository (字典树 统计个数)
- HDU 2846 Repository(字典树,计数)
- 好题 HDU 2846 Repository (字典树)
- 专访关东升:松耦合分层架构设计
- iOS 时间戳
- 自己慢慢学Linux积攒的笔记
- 我的第一篇博客
- 设计模式 - 代理模式
- HDU-2846Repository(字典树)
- 报错:loaded the "" nib but didn't get a UITableView
- OpenSSL 0.9.8g 安装与配置指南
- 微信端和手机qq浏览器输入框不能输入汉字
- OFBIZ-CRUD
- 要理解LINUX内存管理子系统,先要熟悉一些概念
- VMware出现Insufficient permission to access file解决方法
- 用户删除功能
- 制作 Linux ssh key文件登录,登录用户怎么设置