博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2846 Repository
阅读量:7136 次
发布时间:2019-06-28

本文共 1794 字,大约阅读时间需要 5 分钟。

字典树。字典树可以很快的算出某个前缀出现的次数,所以以每个字母作为起点,都插入到字典树中,单词的某一前缀只加一次,加一次之后标记掉,之后不再加。

由于之前没有考虑到查询的单词在字典树中没有出现,导致RE了N次。。。

#include
#include
#include
#include
#include
using namespace std;struct shu{ int summ, nn[27], jihao; }node[500005];int n, i, j, k, ii, tott, zz, q;char s[25], ls[25];int main(){ while (~scanf("%d", &n)) { tott = 1; for (i = 0; i < 500005-5; i++) { memset(node[i].nn, -1, sizeof(node[i].nn)); node[i].summ = 0; node[i].jihao = -1; } for (ii = 0; ii < n; ii++) { scanf("%s", s); int len = strlen(s); for (i = 0; i < len; i++) { zz = 0; for (j = i; s[j]; j++) { if (node[zz].nn[s[j] - 'a'] == -1) { node[zz].nn[s[j] - 'a'] = tott; tott++; } zz = node[zz].nn[s[j] - 'a']; if (node[zz].jihao != ii) { node[zz].summ++; node[zz].jihao = ii; } } } } scanf("%d", &q); for (ii = 0; ii < q; ii++) { int shuchu = 0; scanf("%s", s); zz = 0; for (i = 0; s[i]; i++) { zz = node[zz].nn[s[i] - 'a']; if (zz == -1) { printf("%d\n", 0); shuchu = 1; break; } } if(!shuchu) printf("%d\n", node[zz].summ); } } return 0;}

 

转载于:https://www.cnblogs.com/zufezzt/p/4546488.html

你可能感兴趣的文章
linux 动态库 静态库 函数覆盖
查看>>
nginx代理天地图做缓存解决跨域问题
查看>>
SQL MAP 注入测试
查看>>
Android开机自启动程序
查看>>
php弱类型
查看>>
vim
查看>>
【转载】MapReduce编程 Intellij Idea配置MapReduce编程环境
查看>>
安装配置管理 之 Fedora 6.0 蓝牙bluebooth传送文件的问题解决方法
查看>>
C 结构
查看>>
spring 加载不了jdbc.properties文件的数据问题
查看>>
JQuery Plugin 2 - Passing Options into Your Plugin
查看>>
长尾分布(幂律分布)
查看>>
Android提高--索引
查看>>
队列(Queue)-c实现
查看>>
DevExpress控件使用系列--ASPxGridView+Popup+Tab
查看>>
MySql5.7配置文件my.cnf设置
查看>>
set names utf8;
查看>>
异常处理
查看>>
go学习之文件读取问题(需更新)
查看>>
quartus15.1 下程程序 电脑蓝屏 解决方法
查看>>