题中没给范围 所以控制不好数组范围。。不是超内存就是runtime。。

好吧 到了晚上终于调出来数组模拟的了

题意:

  求含有某字符段的个数

解析:

  把每个字符串遍历一遍 以每个元素为起点建树就好了。。

  注意add型。。因为每个字符串的元素只记一次  所以用id标记一下是否属于同一个源字符串就好了

嗯。。。。还有。。。用c++交。。。

指针:

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin->tie(0)
//freopen("1->txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
char s[];
struct node
{
int num;
int id;
node *next[maxn];
node()
{
num = id = ;
mem(next, );
}
}rt; void build(char *a, int id)
{
int k = ;
node *p = &rt;
while(a[k])
{
// int x = a[k++] - 'a';
if(!p->next[a[k] - 'a']) p->next[a[k] - 'a'] = new node;
p = p->next[a[k++] - 'a'];
if(p->id != id)
p->num++;
p->id = id;
}
} int qp(char *a)
{
int k = ;
node *p = &rt;
// int x = a[k] - 'a';
while(a[k] && p->next[a[k] - 'a']) p = p->next[a[k++] - 'a'];
if(a[k]) return ;
return p->num;
} int main()
{
// init();
int n;
scanf("%d", &n);
rap(i, , n)
{
cin>> s;
int len = strlen(s);
rep(j, , len)
{
build(s+j, i);
}
}
int m;
scanf("%d", &m);
rap(i, , m)
{
cin>> s;
printf("%d\n", qp(s)); } return ;
}

数组:

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int trie[maxn*][], sum[maxn*], id[maxn*];
int rt, tot;
char s[];
void build(char* ss, int p)
{
int len = strlen(ss);
rt = ;
for(int i=; i<len; i++)
{
int x = ss[i] - 'a';
if(trie[rt][x] == )
{
trie[rt][x] = ++tot;
}
rt = trie[rt][x];
if(id[rt] != p)
sum[rt]++;
id[rt] = p;
}
//sum[rt]++;
} int qp()
{
int len = strlen(s);
rt = ;
for(int i=; i<len; i++)
{
int x = s[i] - 'a';
if(trie[rt][x] == ) return ;
rt = trie[rt][x];
}
return sum[rt];
} void init()
{
mem(trie, );
mem(sum, );
mem(id, );
tot = ;
} int main()
{
init();
int n;
scanf("%d", &n);
rap(i, , n)
{
cin>> s;
int len = strlen(s);
rep(j, , len)
{
build(s+j, i);
// cout<< " " << s+j <<endl;
}
}
int m;
scanf("%d", &m);
rap(i, , m)
{
cin>> s;
printf("%d\n", qp()); } return ;
}

Repository HDU - 2846 (trie)的更多相关文章

  1. hdu 4825(Trie)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  2. A * B Problem Plus HDU - 1402 (FFT)

    A * B Problem Plus HDU - 1402 (FFT) Calculate A * B.  InputEach line will contain two integers A and ...

  3. D - 淡黄的长裙 HDU - 4221(贪心)

    D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...

  4. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  5. HDU 1671 Phone List (Trie)

    pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

  7. LA3942-Remember the Word(Trie)

    题意: 有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法 分析: dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len( ...

  8. hdu 5055(坑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5055 Bob and math problem Time Limit: 2000/1000 MS ( ...

  9. hdu 5391 (数论)

    Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

随机推荐

  1. 后缀数组(SA)总结

    后缀数组(SA)总结 这个东西鸽了好久了,今天补一下 概念 后缀数组\(SA\)是什么东西? 它是记录一个字符串每个后缀的字典序的数组 \(sa[i]\):表示排名为\(i\)的后缀是哪一个. \(r ...

  2. 【JUC源码解析】ThreadPoolExecutor

    简介 ThreadPoolExecutor,线程池的基石. 概述 线程池,除了用HashSet承载一组线程做任务以外,还用BlockingQueue承载一组任务.corePoolSize和maximu ...

  3. RestQL:现代化的 API 开发方式

    参考:https://tech.meituan.com/koa-restql.html 在现代的业务系统中,后端开发工作基本上可以被拆分为三项: 接口鉴权.例如判断是不是当前系统的用户,以及该用户是否 ...

  4. 【RAC搭建报错】libcap.so.1:cannot open shared object file

    原文参考:http://blog.csdn.net/siyanyanyanyai/article/details/45306595 http://orax.blog.sohu.com/26207226 ...

  5. 搜索引擎ElasticSearch系列(二): ElasticSearch2.4.4 Head插件安装

    一:ElasticSearch Head插件简介 elasticsearch-head is a web front end for browsing and interacting with an  ...

  6. Selenium2+python自动化-CSS定位语法

    前言 一些人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁.这一篇css的 ...

  7. Unity学习笔记(1)

    transform: transform是GameObject的一个默认的组件,其包含着该对象的几种属性,坐标(Position)以及旋转角度(Rotation)和尺寸(Scale). transfo ...

  8. CSP201609-2:火车购票

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  9. OpenLDAP配置TLS加密传输

    原文发表于cu:2016-07-04 参考文档: 基于OpenSSL自建CA与颁发SSL证书:http://seanlook.com/2015/01/18/openssl-self-sign-ca/ ...

  10. asp.net的forms身份验证 单用户身份验证

    asp.net的forms身份验证  单用户身份验证 首先要配置Web.config文件 <system.web> <authentication mode="Forms& ...