题目大意:给你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. SQLAlchemy中filter()和filter_by()的区别

    1.filter引用列名时,使用“类名.属性名”的方式,比较使用两个等号“==” 2.filter_by引用列名时,使用“属性名”,比较使用一个等号“=” 3.在使用多条件匹配的时候,filter需要 ...

  2. 容器set和multiset

    一.set和multiset基础 set和multiset会根据特定的排序准则,自动将元素进行排序.不同的是后者允许元素重复而前者不允许. 需要包含头文件: #include <set> ...

  3. Clone()方法C#

    class DrawBase:System.Object , ICloneable { public string name = "jmj"; public DrawBase() ...

  4. [转]SSIS中的脚本—脚本任务

    本文转自:http://www.cnblogs.com/tylerdonet/archive/2011/09/16/2179123.html 脚本任务主要用来控制数据流,当现有的控制流 任务不能满足复 ...

  5. META http-equiv="refresh" 实现网页自动跳转

    使用说明: < HEAD> < TITLE>刷新内容< /TITLE> < META HTTP-EQUIV="REFRESH" CONTE ...

  6. Node.js monly图片批量下载爬虫1.00

    此爬虫又用到了iconv转码,代码如下: //====================================================== // mmonly图片批量下载爬虫1.00 ...

  7. Python的不同实现

    这里的实现指的是符合Python语言规范的Python解释程序以及标准库等.这些实现虽然实现的是同一种语言,但是彼此之间,特别是与CPython之间还是有些差别的. 下面分别列出几个主要的实现. 1. ...

  8. Bitmap和Drawable的互相转换

    刚好之前的项目实用到.怕遗忘了.就先记录下来.然后会用到的时候直接来这copy使用就好了. 1.Bitmap ---->Drawable: public static Drawable bitm ...

  9. 解决RegexKitLite编译报错

    原地址:http://blog.csdn.net/kepoon/article/details/7586861 在编译RegexKitLite的时候,报错如下: Undefined symbols f ...

  10. Unity3d 显示IOS基本的游戏中心脚本

    using UnityEngine; using UnityEngine.SocialPlatforms; public class Startup : MonoBehaviour { // we'l ...