HDU 2846 Repository(字典树,标记)
字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org。。。。
我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊。。。。
我用了val来标记是否是同一个串分解而来的,保存的是串的编号
num记录数目。
//string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);
//把[first0,last0)之间的部分替换成[first,last)之间的字符串 #include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
using namespace std;
#define ll __int64
int pos;
struct tt
{
int val,arr[],num;
tt(){
val=-,num=;
memset(arr,-,sizeof(arr));
}
}a[]; //用数组模拟的字典树开的内存大小也是一个技术活,太大超内存,太小runtime errer。。 void insert(char *s,int val,int id,int d)
{
if(s[d]=='\0')return; int t=s[d]-'a';
if(a[id].arr[t]==-)
a[id].arr[t]=++pos;
id=a[id].arr[t];
if(a[id].val!=val)
{
a[id].val=val;
a[id].num++;
}
insert(s,val,id,d+);
}
int solve(char *s,int id)
{
int ans=;
int len=strlen(s);
for(int i=;i<len;i++)
{
int t=s[i]-'a';
if(a[id].arr[t]==-)
return ;
else
id=a[id].arr[t];
}
return a[id].num;
} int main()
{
int t;
pos=;//注意初始化的位置
scanf("%d",&t);
while(t--)
{
char s[];
scanf("%s",s);
int len=strlen(s);
for(int i=;i<len;i++)
{
insert(s+i,t,,);
}
}
int n;
scanf("%d",&n);
while(n--)
{
char s[];
scanf("%s",s);
int ans=solve(s,);
printf("%d\n",ans);
}
return ;
}
HDU 2846 Repository(字典树,标记)的更多相关文章
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- hdu 2846 Repository (字典树)
RepositoryTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- hdu 2846(字典树)
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(字典树)
字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过. #include<io ...
- hdu 2846 Repository
http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1979 DFS + 字典树剪枝
http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
- hdu2846 Repository 字典树(好题)
把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...
随机推荐
- ado.net的5个主要对象
connection 连接对象 command 命令对象,指示要执行的命令和存储过程! datareader是一个向前的只读的数据流. dataadapter是功能强大的适陪器,支持增删改查的功能 d ...
- 发布web项目时,忽略指定文件夹或文件
参考:http://blogs.msdn.com/b/webdev/archive/2010/04/22/web-deployment-excluding-files-and-folders-via- ...
- Microsoft Azure 的一些限制 Global
Azure Subscription and Service Limits, Quotas, and Constraints http://azure.microsoft.com/en-us/docu ...
- 社保系列7《PSAM卡》
PSAM卡用于商户POS.网点终端.直联终端等端末设备上,负责机具的安全控管.PSAM卡具有一定的通用性.经过个人化处理的PSAM卡能在不同的机具上使用. PSAM卡支持多级发卡的机制,各级发卡方在卡 ...
- .NET开源工作流RoadFlow-流程设计-流转条件设置(路由)
当一个步骤后面有多个步骤时,可以设置为根据设置条件系统自动判断该流向哪些步骤,也叫路由. roadflow没有单独的路由步骤来设置条件,流程条件通过双击连线弹出条件设置框来设置. 1.sql条件 即通 ...
- spring AOP advice 类型 和 通用的切点的配置方式
spring aop advice的类型: 1.前置通知(before advice) 2.返回后通知(after returning advice) 3.抛出异常后通知(after throwing ...
- android线程间通讯
近来找了一些关于android线程间通信的资料,整理学习了一下,并制作了一个简单的例子. andriod提供了 Handler 和 Looper 来满足线程间的通信.例如一个子线程从网络上下载了一副图 ...
- 在VS2010 SP1基础上安装mvc3
安装VS2010 SP1后,再安装mvc3会报错,估计原因是此安装包会安装VS的补丁,而sp1的补丁版本高过此安装包的. AspNetMVC3ToolsUpdateSetup.exe 解决办法: 运行 ...
- iTiTa再次回归,这一年我们都在干什么?
转眼一看这都2013年了,距离我们上一次发布篮球游戏已经一年多了. 从去年到今年我们几乎只做了一件事——<二战前线>! 这一年的时间从最简单的游戏游戏雏形到今天一个具备商业能力的游戏,一路 ...
- KafkaOffsetMonitor使用方法
(1)下载jar包 去网上搜索KafkaOffsetMonitor即可. 我这里共享了我的百度云连接:http://yun.baidu.com/s/1nvGjbDn 如果某一天我这个取消共享了,大家去 ...