hdu 2846
字典树的变形,常规字典树用来求前缀的,所以把每个单词拆成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的更多相关文章
- HDU 2846 (AC自动机+多文本匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846 题目大意:有多个文本,多个模式串.问每个模式串中,有多少个文本?(匹配可重复) 解题思路: 传统 ...
- hdu 2846 Repository
http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 2846:Repository(Trie)
http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 2846(字典树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2846 Repository(字典树,标记)
题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...
- HDU 2846 Repository(字典树)
字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过. #include<io ...
- HDU 2846 Trie查询
给出若干模式串,再给出若干询问串,求每个询问串作为多少个模式串的子串出现. 如果一个串是另一个串的子串,则一定是另一个串某个前缀的后缀或者某个后缀的前缀.根据字典树的性质,将模式串的每一个后缀插入字典 ...
随机推荐
- UVA 10400 Game Show Math (dfs + 记忆化搜索)
Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...
- ie8 hack
1.‘\9’: eg:.test { color/*\**/: blue\9 }.header {width:300px;} /* 所有浏览器*/.header {width/*\**/:330px\ ...
- (转)javascript组件开发方式
作为一名前端工程师,写组件的能力至关重要.虽然javascript经常被人嘲笑是个小玩具,但是在一代代大牛的前仆后继的努力下,渐渐的也摸索了一套组件的编写方式. 下面我们来谈谈,在现有的知识体系下,如 ...
- SQL数据库注入防范 ASP.NET Globle警告
在项目中的Global.asax页面代码中加下面的代码,就可以有效的防范简单的SQL注入. protected void Application_BeginRequest(Object sender, ...
- Asp.Net Mvc5新特性
One ASP.NET:统一平台 BootStrap:免费Css响应式页面 路由标记属性:简单,控制器,操作,前缀,参数,URL ASP.NET WEB API 2:路由标记属性,Oauth2.0,O ...
- tableview 分割线置最左边的解决方法
首先在viewDidLoad方法加入以下代码: if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [se ...
- VC进程提权
如果我们想读取目标进程中的内存 就需要将进程提权 否则访问失败. 下面是提权代码 #include <TlHelp32.h> /****************************** ...
- 路由转发(curl)
<?php ini_set('memory_limit', '640M'); ini_set('default_charset', 'utf-8'); define('webroot', 'ht ...
- js中的两个数字a,b求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。一共b个数字相加,例如用户输入2,5 s=2+22+222+2222+22222
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- MFC中使用ADO方式连接数据库
文章转自:http://blog.sina.com.cn/s/blog_a43aba5601014z8h.html 一.数据库操作准备 1.导入ADO动态链接库 在工程的stdafx.h中加入如下语句 ...