题目大意:给你n个字符串,然后给你m个子串,看这个子串在上面的多少个串中,出现过;

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846

本题可以在字典树上进行改进;

AC代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn = ;
typedef struct node
{
int count;
int id;
node *next[maxn];
}trie;
void inser(trie *root,char *s,int id)
{
int i,index;
trie *p = root;
for(i = ;s[i];i++)
{
index = s[i]-'a';
if(p->next[index] == NULL)
{
trie *temp= (trie *)malloc(sizeof(trie));
for(int j=;j<maxn;j++)
{
temp->next[j] = NULL;
}
temp->count = ;
temp->id = -;
p->next[index] = temp;
}
p=p->next[index];
if(p->id != id)
{
p->id = id;
p->count++;//统计每个出现的个数;
}
}
}
int search(trie *root,char *s)
{
trie *p=root;
int i,index;
for(i=;s[i];i++)
{
index = s[i]-'a';
if(p->next[index] == NULL) return ;
p=p->next[index];
}
return p->count;
}
int main(int argc, char *argv[])
{
int i,j;
int n,m;
char s[];
trie *root = (trie *)malloc(sizeof(trie));//清理内存,初始化;
for(i=;i<maxn;i++)
{
root->next[i] = NULL;
}
root->count = ;
root->id = -;
root->count = ;
root->id = -;
scanf("%d",&n);
for(i =;i<n;i++)
{ scanf("%s",s);
for(j =;j<strlen(s);j++)
{
inser(root,s+j,i);
}
}
scanf("%d",&m);
for(i=;i<m;i++)
{
scanf("%s",s);
printf("%d\n",search(root,s));
}
return ;
}

代码二:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
struct node
{
int count, id;
node *childs[];
node(){
id=-;
count=;
int i;
for(i=; i<; i++)
childs[i]=NULL;
}
};
node *root=new node;
node *current, *newnode;
void insert(char *str, int d)
{
int i, m;
current=root;
for(i=; i<strlen(str); i++)
{
m=str[i]-'a';
if(current->childs[m]!=NULL)
{
current=current->childs[m];
}
else{
newnode=new node;
current->childs[m]=newnode;
current=newnode;
}
if(current->id!=d)
{
current->id=d;
++(current->count);
}
}
}
int search(char *str)
{
//printf("%s",str);
int i, m;
current=root;
for(i=; i<strlen(str); i++)
{
m=str[i]-'a';
if(current->childs[m]==NULL)
return ;
current=current->childs[m];
}
return current->count;
} int main()
{
int i, t, s, k;
char a[];
scanf("%d",&t);
s=;
while(t--){
scanf("%s",a);
s++;
for(i=; i<strlen(a); i++)
insert(a+i, s);
}
scanf("%d",&k);
while(k--){
scanf("%s",a);
printf("%d\n",search(a));
}
return ;
}

poj 2846 Repository的更多相关文章

  1. hdu 2846 Repository

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

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

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

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

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

  4. hdu 2846 Repository (字典树)

    RepositoryTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

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

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

  6. HUD 2846 Repository

    /* 开始想耍小聪明 直接map搞 代码短 好理解 空间够 恩 很好 就是 map慢死了 T了 */ #include<iostream> #include<cstdio> # ...

  7. HDU 2846 Repository(字典树)

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

  8. HDU:2846-Repository

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

  9. HDU 2846:Repository(Trie)

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

随机推荐

  1. eclipse 性能调优之内存分配

    转自:http://blog.csdn.net/defonds/article/details/6289236 如果觉得自己的 eclipse 比较慢,可以通过修改 %eclipse_home%/ec ...

  2. 表单对象属性disabled和readOnly

    简而言之: disabled 和 readonly 区别: disabled 被禁用后的元素,不会随表单提交 readonly 不可修改, 会随表单提交

  3. [Java基础] java的守护线程与非守护线程

    最近重新研究Java基础知识,发现以前太多知识知识略略带过了,比较说Java的线程机制,在Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) ,(PS:以 ...

  4. iOS:CALayer锚点的使用

    CALayer层的位置主要和position和anchorPoint有关.其中它们在一起才能决定层在视图中的具体位置. @property CGPoint position;         //位置 ...

  5. Win8安装程序出现2502、2503错误解决方法

    我是在安装VMware virtualbox的时候遇到的这个问题,上网百度了一下发现这是个在win8系统上安装程序时才会遇到的. 究其原因这个问题还是由于权限问题导致的,解决方法如下: 1,ctrl+ ...

  6. request.startAsync()不支持异步操作

    Servlet3.0使用异步处理时,后台报错: java.lang.IllegalStateException: A filter or servlet of the current chain do ...

  7. http://blog.csdn.net/xingfuzhijianxia/article/details/6433918

    http://blog.csdn.net/xingfuzhijianxia/article/details/6433918

  8. Hive不等值连接

    select * from ( select t1.instalment_id as r_id , t2.instalment_id as p_id from (select instalment_i ...

  9. JMS之——ActiveMQ 高可用与负载均衡集群安装、配置(ZooKeeper + LevelDB + Static discovery)

    一.说明 从 ActiveMQ 5.9 开始, ActiveMQ 的集群实现方式取消了传统的 Master-Slave 方式,增加了基于ZooKeeper + LevelDB 的 Master-Sla ...

  10. ActiveMQ订阅模式持久化实现

    实现步骤:1.配置发送xml,applicationContext-send.xml <?xml version="1.0" encoding="UTF-8&quo ...