HDU2846
来源:互联网 发布:网络问政直通车 编辑:程序博客网 时间:2024/06/06 01:12
【HDU2846】Repository
如果输入字符串abbcc,那就将abbcc bbcc bcc cc c都插入字典树中
字符串adad是一个特殊的存在,需要标记一下
一开始我用的是数组版的字典树,一直超时,用静态指针就过了
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <string>using namespace std;struct node{int count;int num;node *childs[26];node(){count=0;num=0;for(int i=0;i<26;i++){childs[i]=NULL;}}};node *root=new node;node *current,*newnode;void insert(char *s,int k){current=root;int len=strlen(s);for(int i=0;i<len;i++){int m=s[i]-'a';if(current->childs[m]!=NULL){current=current->childs[m];if(current->num!=k){++(current->count);current->num=k;}}else{newnode=new node;++(newnode->count);newnode->num=k;current->childs[m]=newnode;current=newnode;}}}int find(char *s){current=root;int len=strlen(s);for(int i=0;i<len;i++){int m=s[i]-'a';if(current->childs[m]==NULL) return 0;current=current->childs[m];}return current->count;}int main(){char s1[25];int n,m;scanf("%d",&n);int li=0;while(n--){li++;scanf("%s",s1);int len=strlen(s1);for(int i=0;i<len;i++){insert(s1+i,li);}}scanf("%d",&m);for(int i=0;i<m;i++){scanf("%s",s1);printf("%d\n",find(s1));}return 0;}
0 0
- hdu2846
- HDU2846
- hdu2846
- hdu2846
- hdu2846 Repository
- HDU2846-Repository
- HDU2846-Repository
- HDU2846 Repository, Trie树
- hdu2846 Repository (字典树)
- hdu2846 Repository 字典树
- hdu2846(字典树好题)
- hdu2846 字典树 判子串
- HDU2846——Repository
- HDU2846 Repository 字典树
- 字典树求子串hdu2846
- hdu2846 Repository (字典树)
- hdu2846 Repository -字典树
- HDU2846【字典树】
- android拷贝数据库
- 数据结构和算法学习(9)-红黑树
- js获取页面元素的位置
- 编码解码
- Xcode 【用Alcatraz去管理插件(新版本采用第二种终端执行方法即可)】
- HDU2846
- java redis学习备忘
- java与C语言在字符串结束符上的区别
- 最长递增子序列-动态规划
- Windows 内存绘图导出RGB
- JSP内置对象request
- 大型网站之分布式会话管理
- 输入法的相关调用
- Apps must follow the iOS Data Storage Guidelines or they will be rejected