博客
关于我
P3879 [TJOI2010]阅读理解
阅读量:337 次
发布时间:2019-03-04

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

题目

制作一个数据结构,支持静态询问字符串x出现在哪些短文中并去重。

思路

首先,我们需要一个高效的数据结构来存储字符串x及其出现的位置。为了实现查找功能,我们可以使用std::map来存储键为字符串x,值为一个std::set

的映射。std::set不仅可以自动去重,还能快速查找元素是否存在,这样我们就能快速判断x是否出现在文本中。通过这种方式,我们可以在O(log n)的时间复杂度内完成查找和插入操作。

#include 
#include
#include
#include
#include
#include
using namespace std;map
> a;int n, m;string x;int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> m; for (int j = 0; j < m; ++j) { cin >> x; a[x].insert(i + 1); } } cin >> n; for (int i = 0; i < n; ++i) { cin >> x; if (a.find(x) != a.end()) { for (set
::iterator it = a[x].begin(); it != a[x].end(); ++it) { cout << *it << ' '; } } } return 0;}

转载地址:http://vfye.baihongyu.com/

你可能感兴趣的文章
PHP -算法-二路归并
查看>>
php 2条不一样 的json数据 怎么放在一个json里面_如果你是PHP开发者,请务必了解一下Composer...
查看>>
php 360 不记住密码,JavaScript_多种方法实现360浏览器下禁止自动填写用户名密码,目前开发一个项目遇到一个很 - phpStudy...
查看>>
regExp的match、exec、test区别
查看>>
php 404 自定义,APACHE 自定义404错误页面设置方法
查看>>
PHP 5.3.0以上推荐使用mysqlnd驱动
查看>>
php 7.2 安装 mcrypt 扩展: mcrypt 扩展从 php 7.1.0 开始废弃;自 php 7.2.0 起,会移到 pecl...
查看>>
php aes sha1解密,PHP AES加密/解密
查看>>
php array 分片,PHP常用数组函数小结
查看>>
php CI框架单个file表单多文件上传例子
查看>>
php composer
查看>>
reflow和repaint引发的性能问题
查看>>
Reflection反射机制原理、使用场景 及 缺陷
查看>>
php csv 导出
查看>>
php curl 实例+详解
查看>>
php curl_init函数用法(http://blog.sina.com.cn/s/blog_640738130100tsig.html)
查看>>
php curl_multi批量发送http请求
查看>>
php curl请求微信发红包接口出现错误:Peer's Certificate issuer is not recognized.
查看>>
PHP curl请求错误汇总和解决方案
查看>>