poj 2846 Repository
题目大意:给你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的更多相关文章
- hdu 2846 Repository
http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- 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 (字典树)
RepositoryTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 2846 Repository(字典树,标记)
题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...
- HUD 2846 Repository
/* 开始想耍小聪明 直接map搞 代码短 好理解 空间够 恩 很好 就是 map慢死了 T了 */ #include<iostream> #include<cstdio> # ...
- 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 2846:Repository(Trie)
http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...
随机推荐
- select标签中option内容加链接
1.Html页面代码 <select name="select" id="select" style="height: 25px; width: ...
- 客户端实现负载均衡:springCloud Ribbon的使用
Netfilx发布的负载均衡器,是一个基于http.tcp的客户端负载均衡工具,具有控制http.tcp客户端的行为,为ribbon配置服务提供者的地址后,ribbon就 可以经过springClou ...
- ecshop ecmall shopex
ecshop 是一个B2C商城 适合企业及个人快速构建个性化网上商店.系统是基于PHP语言及MYSQL数据库构架开发的跨平台开源程序.(如沃购网) 山大路是dedecms ecmall(ECMall ...
- Oracle 命令导入数据
1.用命令进入sqlplus: sqlplus 用户名:/orcl 2.执行sql文件 sql>@D:/lanxi_his_data/V_JH_VISITINFO.sql
- phantomjs 无法打开https网站解决方案
最近测试原来的爬虫程序,发现phantomjs 无法打开https网站了,经过网上查下,发现需要在phantomjs定义的加以下参数 self.driver = webdriver.PhantomJS ...
- phpcms的后台网站直接访问正常,百度快照收录链接访问跳转到非法网站
问题: phpcms制作的网站直接访问正常,百度快照收录链接访问跳转到非法网站 百度快照收录网站域名,访问时自动跳转到一个非法网站 检查静态页index.html,index.php 网页内引用 ...
- Spark Streaming资源动态分配和动态控制消费速率
本篇从二个方面讲解: 高级特性: 1.Spark Streaming资源动态分配 2.Spark Streaming动态控制消费速率 原理剖析,动态控制消费速率其后面存在一套理论,资源动态分配也有一套 ...
- 【Other】ASCII Text Art
http://fsymbols.com/cn/wenzi-yishu/ http://fsymbols.com/text-art/#all_cats http://chris.com/ascii/in ...
- Spring框架学习(8)spring mvc上传下载
内容源自:spring mvc上传下载 如下示例: 页面: web.xml: <?xml version="1.0" encoding="UTF-8"?& ...
- 【转】编译安装PHP并配置PHP-FPM
1.前言上一篇讲述了如何编译安装MySQL,虽然可以通过yum install 或者rpm来安装,但是yum install和rpm安装有一个特点,就是有些参数是别人根据大众需求定制的,如果需要进行自 ...