字典树的变形,常规字典树用来求前缀的,所以把每个单词拆成len个词建树,为了避免abab这样的查ab时会出现两次,每次加一个标记,如果该节点上次的建树的单词与本次相同就不更新,否则更新

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tree
{
struct tree *son[26];
int count;
int flag;
}*root;
void insert(char *p,int id)
{
int i,k,j;
tree *cur=root,*next;
int len=strlen(p);
for(i=0;i<len;i++)
{
k=p[i]-'a';
if(cur->son[k]!=NULL)
cur=cur->son[p[i]-'a'];
else
{
next=new(tree);
for(j=0;j<26;j++)
next->son[j]=NULL;
next->count=0;
next->flag=-1;
cur->son[p[i]-'a']=next;
cur=next;
}
if(cur->flag!=id)//如果上次在该节点更新的单词与本次单词不同就更新
{
cur->count++;
cur->flag=id;
}
}
}
int find(char *p)
{
int i;
tree *cur=root;
int len=strlen(p);
for(i=0;i<len;i++)
{
int k=p[i]-'a';
if(cur->son[k]==NULL)
break;
else cur=cur->son[k];
}
if(i<len) return 0;
return cur->count;
}
int main()
{
int i,j,n,m;
char s[25];
root=new(tree);
for(i=0;i<26;i++)
root->son[i]=NULL;
root->count=0;
root->flag=-1;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",s);
for(j=0;s[j];j++)
{
insert(s+j,i);//拆单词建树
}
}
scanf("%d",&m);
while(m--)
{
scanf("%s",s);
j=find(s);
printf("%d\n",j);
}
return 0;
}

hdu 2846的更多相关文章

  1. HDU 2846 (AC自动机+多文本匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846 题目大意:有多个文本,多个模式串.问每个模式串中,有多少个文本?(匹配可重复) 解题思路: 传统 ...

  2. hdu 2846 Repository

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others)     ...

  3. HDU 2846:Repository(Trie)

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...

  4. HDU 2846 Repository (字典树 后缀建树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  5. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. hdu 2846(字典树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. HDU 2846 Repository(字典树,标记)

    题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...

  8. HDU 2846 Repository(字典树)

    字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过. #include<io ...

  9. HDU 2846 Trie查询

    给出若干模式串,再给出若干询问串,求每个询问串作为多少个模式串的子串出现. 如果一个串是另一个串的子串,则一定是另一个串某个前缀的后缀或者某个后缀的前缀.根据字典树的性质,将模式串的每一个后缀插入字典 ...

随机推荐

  1. [ES6] Generators

    Example 1: function *topicList(){ yield "ES2015"; yield "Semi-colons: good or bad?&qu ...

  2. android studio 报ambiguous method call

    如题,在android studio中调用this.toString时,提示的错误信息是ambiguous method call. both get class () in object and g ...

  3. ORACLE查看数据文件-控制文件-日志文件-表空间信息

    1.查看当前数据库中的所有用户:select username from dba_users; 2.查看当前会话登录的用户:show user或select username from user_us ...

  4. mysqldump备份原理

    现网中数据库运维时,要经常对数据库做热备.为保证恢复时数据的完整性与一致性, 一种方法是在备份之前锁表,但锁表会影响正在运行的业务. mysqldump是当前MySQL中最常用的备份工具,通过mysq ...

  5. UVA11388 GCD LCM1 2 -1

    题目: 给你两个数G和L,求a和b,他们的最大公约数为G和最小公倍数为L,输出a最小时的a和b.如果不存在在输出-1. Sample Input   2 1 2 3 4 Output for Samp ...

  6. Android与JS混编(多图选择器)

       github: https://github.com/weifengzz/AndroidJSSelectImg

  7. IE6 浏览器常见兼容问题 大汇总(23个)

    IE6以及各个浏览器常见兼容问题 大汇总 综述:虽然说IE6在2014年4月将被停止支持,但是不得不说的是,IE6的市场并不会随着支持的停止而立刻消散下去,对于WEB前端开发工程师来说,兼容IE6 兼 ...

  8. 给定范围内产生N个不同的随机数

    void RandNumbs(int nLimts, int result[], int n)//给定范围内产生n个不同随机数(1-nLimts),并存储到result中 { int nNum = 0 ...

  9. Ruby和Rails开发环境安装

    更新包管理 sudo apt-get update 安装curl sudo apt-get install curl *安装rvm via curl \curl -L https://get.rvm. ...

  10. POJ1015 动态规划

    POJ1015 问题重述: 在n个候选者中选取m个人进入陪审团.每个候选者获得两项评分:D[j],P[j].求解最佳评审团,使得在每个成员的两项评分和之差 最小的情况下,使得两项评分和之和 最大. 分 ...