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个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...
随机推荐
- !!! # @ --- ODATA 云驱动 --- 数据库发布 RESTFUL API
Cloud Drivers ODATA 云驱动 http://www.cdata.com/cloud/ Makes on-premise & cloud data sources ea ...
- text-align 属性,输入框数字向右靠
1.业务需求:金额输入框数字向右靠 2.HTML文件 <td id="otherPay_Td"> <input id="otherPay" t ...
- Vue脚手架(vue-cli)搭建和目录结构详解
一.环境搭建 1.安装node.npm.webpack,不多说 2.安装vue-cli脚手架构建工具,打开命令行工具输入:npm install vue-cli -g,安装完成之后输入 vue -V( ...
- Linux系统443端口被占用无法启动解决办法
etstat -ano|findstr "443" //搜索443端口占用情况,并找到进程IDTCP 0.0.0.0:443 0.0.0.0:0 LISTENING ...
- 往aws中的s3上传数据
在官网下载对应的sdk http://aws.amazon.com/cn/sdk-for-net/ 然后再aws上为s3上的bucket生成密钥对 access_key和secret_key 调用对应 ...
- (转)如何将本地git仓库中的代码上传到github
1, 在github上新建一个仓库,比如为:CSS3Test,仓库地址为:https://github.com/hyuanyuanlisiwei/CSS3Test 2,本地git仓库中的文件项目为C ...
- jsp页面数据回显(select下拉选择框)
1.静态变量方式: <!-- 实现select标签回显 --> 1.<select name="curStatus" value="${curStatu ...
- DevExpress 项目目录列表参考(收集的 350个cs project)
DevExpress.ExpressApp.Tools\DBUpdater\DBUpdater.csproj DevExpress.BonusSkins\DevExpress.BonusSkins.c ...
- android 带文字阴影的button
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- 如何使用angularjs实现表单验证
<!DOCTYPE html> <html ng-app="myApp"> <head> <title>angularjs-vali ...