题目链接:点击查看

题目大意:给出n个电话号码,每个电话号码都由9位数字组成,我们需要输出每个电话号码的最小关键词,最小关键词是指当输入这个关键词后,只能与当前的电话号码的其中一段匹配,而不能和其他电话号码的子字符串匹配

题目分析:这个题可以直接暴力做,但我一开始没敢。。去网上搜了一下题解发现可以暴力于是才敢的,其实也没什么大不了,自己计算一下,一个九位数的电话号码的连续子串一共就只有9+8+7+6+5+4+3+2+1=45种,其长度之和也是等于45,所以一开始我们先用n*45的时间将n个电话号码都存到字典树中,然后依次枚举每个电话号码,操作的具体过程如下:

  1. 从字典树中将其45个子字符串删掉
  2. 从长度由小至大枚举45个子字符串,若有满足当前子字符串在字典树中没出现过,返回退出
  3. 将45个子字符串重新添加到字典树中

按照这个过程暴力枚举即可,这里需要提一下关于字典树的数组大小,因为是个二维数组,我们可以描述为trie[节点数][字符数],可以更进一步的描述为trie[字符串数*字符串长度][字符数],这样一来这个题目一共有n个字符串,每个字符串可以分成的子字符串的长度之和是45,所以开7e4*45*26就够了,再大的话评测机不给过

还有就是一开始在纠结该如何记录每个子串,用了一个布尔变量来记录,结果发现每次删除都会将其他电话号码中的相同子串删除掉,有点自闭,后来才想到,用一个sum数组记录每个节点出现的次数即可,也就是储存了每个子字符串出现的次数,每次删除让该子字符串对应的节点减一即可,恢复的话就是加一

然后这个题目因为都有减一和加一了,而且还是字符串,我们不难想到可以用映射来做这个题,给出的还是一个九位数的数字,我们可以选择用map<int,int>来映射,也可以为了操作方便,用map<string,int>来映射,然后这是一个cf的题目,支持c++11,可以果断上cin加速外挂和无序map来完成上述操作

最后总结一下吧,大概就是字典树在这个题目中的贡献有点像是用空间换时间,字典树跑这个题用了400多ms,无序map乱搞的用了2000多ms,然后用了cin加速外挂的话,切记不能再用printf和scanf了,会莫名其妙的RE,TLE和WA

代码:

字典树:405ms,137.5MB

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
using namespace std;typedef unsigned long long ull;typedef long long LL;const int inf=0x3f3f3f3f;const int N=7e4+100;int trie[N*45][10];int sum[N*45];int cnt=0;string s[N];void insert(string s)
{int pos=0;for(int i=0;i<s.size();i++){int to=s[i]-'0';if(!trie[pos][to])trie[pos][to]=++cnt;pos=trie[pos][to];}sum[pos]++;
}void del(string s)
{int pos=0;for(int i=0;i<s.size();i++)pos=trie[pos][s[i]-'0'];sum[pos]--;
}bool search(string s)
{int pos=0;for(int i=0;i<s.size();i++)pos=trie[pos][s[i]-'0'];return !sum[pos];
}string solve(int pos)
{for(int len=1;len<=9;len++)for(int i=0;i+len<=9;i++)del(s[pos].substr(i,len));for(int len=1;len<=9;len++)for(int i=0;i+len<=9;i++)if(search(s[pos].substr(i,len))){for(int len=1;len<=9;len++)for(int i=0;i+len<=9;i++)insert(s[pos].substr(i,len));return s[pos].substr(i,len);}
}int main()
{
//  freopen("input.txt","r",stdin);ios::sync_with_stdio(false);int n;cin>>n;for(int i=1;i<=n;i++){cin>>s[i];for(int len=1;len<=9;len++)for(int j=0;j+len<=9;j++)insert(s[i].substr(j,len));}for(int i=1;i<=n;i++)cout<<solve(i)<<endl;return 0;
}

unordere_map:2666ms,42.3MB

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=7e4+100;string s[N];unordered_map<string,int>mp;string solve(int pos)
{for(int len=1;len<=9;len++)for(int i=0;i+len<=9;i++)mp[s[pos].substr(i,len)]--;for(int len=1;len<=9;len++)for(int i=0;i+len<=9;i++)if(!mp[s[pos].substr(i,len)]){for(int len=1;len<=9;len++)for(int i=0;i+len<=9;i++)mp[s[pos].substr(i,len)]++;return s[pos].substr(i,len);}
}int main()
{
//  freopen("input.txt","r",stdin);ios::sync_with_stdio(false);int n;cin>>n;for(int i=1;i<=n;i++){cin>>s[i];for(int len=1;len<=9;len++)for(int j=0;j+len<=9;j++)mp[s[i].substr(j,len)]++;}for(int i=1;i<=n;i++)cout<<solve(i)<<endl;return 0;
}

CodeForces - 858D Polycarp's phone book(字典树/map)相关推荐

  1. Codeforces 861D - Polycarp's phone book 字典树/hash

    输入7e4个字符串,要求每个串提取一个子串来唯一表示 4s题可以hash暴力水过,大体思路就是把所有子串map自己的母串,过程中如果这个子串已有hash值就标-1 然后枚举map元素,维护最小化一下就 ...

  2. CodeForces 858C Did you mean... 、 CodeForces 858D Polycarp's phone book!黑科技

    C. Did you mean... 题意:将一个字符串最少分成几个小的字符串,使得每个字符串都没有连续三个及以上的辅音字母,连续三个相同的辅音字母不算. 贪心构造即可,注意连续三个相同的辅音的时候将 ...

  3. Codeforces 455B A Lot of Games(字典树+博弈)

    题目连接: Codeforces 455B A Lot of Games 题目大意:给定n,表示字符串集合.给定k,表示进行了k次游戏,然后是n个字符串.每局开始,字符串为空串,然后两人轮流在末尾追加 ...

  4. CodeForces - 456D A Lot of Games(字典树+博弈)

    题目链接:点击查看 题目大意:给出n个字符串,现在有两个人玩一个游戏,游戏规则是两人轮流构造同一个字符串,每次可以向末尾添加一个字母,必须保证添加字母后的字符串是n个字符串其中之一的前缀,不能操作者算 ...

  5. CodeForces - 888G Xor-MST(贪心+字典树+最小生成树)

    题目链接:点击查看 题目大意:给出 nnn 个点,任意两个点之间的边权为 ai⊕aja_i\oplus a_jai​⊕aj​,求最小生成树 题目分析:去年多校写过一样的模型,再拿出来写一遍回顾一下:牛 ...

  6. CodeForces - 1476E Pattern Matching(字典树+拓扑)

    题目链接:点击查看 题目大意:给出 nnn 个模式串和 mmm 个匹配串,题目要求输出一种模式串的排列方式,使得 mmm 个模式串从头开始匹配的话,可以匹配到相应的模式串 模式串的长度不超过 444, ...

  7. codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)

    题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...

  8. 【CodeForces】445B A Lot of Games 字典树博弈

    传送门:[CodeForces]445B  A Lot of Games 题目大意:两人一起构造一个串,每人每次向串的末尾放一个字母,必须保证放了这个字母后能够成所给的N个串的前缀,如果某个人不能放时 ...

  9. Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或)

    Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或) 题意: 3种操作: 1 插入一个数 2 删除一个数 3 给出一个数 ...

最新文章

  1. 为栈实现高效的max操作
  2. golang go build 报错 import cycle not allowed
  3. php queryList函数,QueryList/QueryList.php at master · baijunyao/QueryList · GitHub
  4. android studio lambda插件,在Android Studio中使用Lambda表达式(retrolambda)
  5. 如何在Python Django中处理用户身份验证
  6. 鼠标移入时闪闪发光的效果
  7. 20165204 Java第六周学习
  8. jsp校园二手交易平台的设计答辩PPT模板
  9. Linux安装redis(6.0.9)环境
  10. 施乐s2110进入维修模式,富士施乐s2110恢复出厂
  11. 微软笔试题-c语言-算法分析
  12. node 生成随机头像_给微信设置卡通头像,再不怕撞脸!
  13. 美国服务器怎么样 RAKsmart美国服务器适合做什么
  14. [PHP]学生成绩管理系统
  15. ORACLE幻读(一)
  16. C++基础2:ASC码中 ‘A’ 和 ‘a’ 分别在什么位置??
  17. libuv源码分析(1)事件循环分析
  18. 0、PIC系列参考手册中文版文档汇总
  19. 【报告分享】百度创新营销资源整合方案(2021)-百度营销研究院(附下载)
  20. matlab绘制sintsinwt,sin(wt)中的t单位是

热门文章

  1. 基于session认证
  2. MySQL高级 - like模糊匹配
  3. 任务执行者EventLoop
  4. 分布式架构下常见序列化技术-了解序列化的发展
  5. mybatis-一对一的关联查询有两种配置方式
  6. 确认订单 - 提交并且接受订单信息
  7. AOP日志-域对象创建与基本操作介绍
  8. 包-封装模块、设置__init__和外界导入包
  9. plsql(轻量版)_游标的使用1
  10. Spring Cloud依赖