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个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...
随机推荐
- javascript 获取http头信息
Javascript中跟response header有关的就两个方法: getResponseHeader 从响应信息中获取指定的http头 语法 strValue = oXMLHttpReques ...
- localstorge的缓存写法(超过一定时间自动清空)
使用缓存: (设置缓存,尽量用大写,下划线的写法) const ls = { set: function (variable, value, ttl_ms) { var data = {value: ...
- 新手遇到的问题:Easy UI的对话框老是在页面载入完毕后自己主动弹出
因为是第一次接触Easy UI,还不是非常熟悉,尝试了一下对话框功能,还是非常不错的.但问题是页面载入完毕后.全部的对话框都自己主动弹出来了,百度了好久,也没有详细说明确的,貌似别人都没有这个问题哦 ...
- linux:ls、ls -l、ls -al区别 示例
linux:ls.ls -l.ls -al区别 示例 比如test文件夹下有一个test文件.一个.文件夹.一个..文件夹. 则,执行三个命令后,显示效果如下: [root@linuxserver t ...
- 在MySQL中使用子查询和标量子查询的基本用法
一.MySQL 子查询 子查询是将一个 SELECT 语句的查询结果作为中间结果,供另一个 SQL 语句调用.MySQL 支持 SQL 标准要求的所有子查询格式和操作,也扩展了特有的几种特性.子查询没 ...
- C# format 日期 各种 符号 实例分析如何精确C#日期格式到毫秒
摘 自: http://developer.51cto.com/art/200908/141145.htm 实例分析如何精确C#日期格式到毫秒 2009-08-03 10:48 paulfzm jav ...
- android适配的努力
1. 优测 http://utest.qq.com/ 2. 云测 3. 阿里云移动 4. 百度移动测试.
- javascript闭包传参就这么简单
var query = (function (a) { return a; })('fx'); alert(query);
- CSS3文本溢出
text-overflow: text-overflow:clip | ellipsis; clip:剪切多余的文字. ellipsis:文本溢出时显示省略标记. 要实现文本溢出剪切显示省略标记,还需 ...
- Errors occurred during the build
Errors occurred during the build.Errors running builder 'Integrated External Tool Builder' on projec ...