CCPC2020(绵阳) C. Code a Trie
C. Code a Trie
#include<bits/stdc++.h>
#define clean(x) memset(x,0,sizeof(x))
#define fil(x,n) fill(x,x+1+n,0)
#define inf 2000000009
#define maxn 500005
#define int long long
using namespace std;int read()
{int x=1,res=0;char c=getchar();while(c<'0'||c>'9'){if(c=='-')x=-1;c=getchar();}while(c>='0'&&c<='9'){res=res*10+(c-'0');c=getchar();}return res*x;
}int tot=1,ch[maxn][26];
int cnt=0,text_num;
char a[maxn];
vector<int>g[maxn];
map<int,int>mp[maxn],ss;
int f[maxn][25],dep[maxn],ok[maxn],ff[maxn];
void dfs(int x) {for(int i=0;i<=20;i++) f[x][i+1]=f[f[x][i]][i];for(int i=0;i<=25;i++) {int v=ch[x][i];if(v) {f[v][0]=x;dep[v]=dep[x]+1;dfs(v);ok[x]=ok[x]+ok[v];}}
}int lca(int a,int b) {if(dep[a]<dep[b]) swap(a,b);for(int i=20;i>=0;i--) {if(dep[f[a][i]]>=dep[b]) a=f[a][i];}if(a==b) return a;for(int i=20;i>=0;i--) {if(f[a][i]!=f[b][i]) {a=f[a][i];b=f[b][i];}}return f[a][0];
}void insert(int len,int val) {int u=1;for(int i=1;i<=len;i++) {int c=a[i]-'a';if(!ch[u][c]) ch[u][c]=++tot;u=ch[u][c];mp[u][val]=1;}g[val].push_back(u);
}void clear() {for(int i=1;i<=tot;i++) mp[i].clear(),ok[i]=0,ff[i]=0;for(int i=1;i<=tot;i++) for(int j=0;j<=25;j++) ch[i][j]=0;for(int i=0;i<=cnt;i++) g[i].clear();ss.clear();
}void solve()
{int n=read();tot=1;cnt=0;for(int i=1;i<=n;i++) {cin>>(a+1);int len=strlen(a+1);int val=read();if(ss[val]==0) ss[val]=++cnt;val=ss[val];insert(len,val);}dep[1]=1;dfs(1);for(int i=1;i<=cnt;i++) {int lc=-1;for(int k:g[i]) {if(lc==-1) lc=k;else lc=lca(lc,k);}if(ok[lc]) {printf("Case #%d: -1\n",++text_num);clear();return;}ok[lc]=1;ff[lc]=1;for(int j=0;j<=25;j++) {int c=ch[lc][j];if(c==0||mp[c][i]==0) continue;if(mp[c][i]==1&&mp[c].size()>1) {printf("Case #%d: -1\n",++text_num);clear();return;} }}dfs(1);int ans=0;for(int i=1;i<=tot;i++) if(ok[i]) ans++;for(int i=1;i<=cnt;i++) {int lc=-1;for(int k:g[i]) {if(lc==-1) lc=k;else lc=lca(lc,k);}while(lc!=1&&ok[lc]==1&&ff[f[lc][0]]==0) {lc=f[lc][0];ans--;ff[lc]=1;}}printf("Case #%d: %d\n",++text_num,ans);clear();
}signed main()
{int t=read();while(t--)solve();return 0;
}
CCPC2020(绵阳) C. Code a Trie相关推荐
- C. Code a Trie(Trie+dfs+贪心)
C. Code a Trie 大佬题解,代码基本就是抄的 对于每一个值计算所有串的LCA,也就是最长公共前缀,将该节点(Trie树的节点)标记,对于这些字符串在LCA下面的点一定不存在(如果存在他们不 ...
- [CCPC2020 绵阳]G. Game of Cards
Portal 博弈题.并不是 s g sg sg题,而且直接的脑子博弈.设输入为 a , b , c , d a,b,c,d a,b,c,d. 首先若 b = c = d = 0 b=c=d=0 b= ...
- 2020CCPC绵阳
2020CCPC绵阳 题号 题目名 难度 知识点 A A Colorful Grid B Building Blocks C Code a Trie D Defuse the Bombs 签到 二分 ...
- HDU2896 病毒侵袭
题目传送门 AC自动机第一题~ 一看就是一个非常简单的多串匹配问题了,输出方案?记录一下就好了 注意这里code是Trie图,它是AC自动机的改进版本,有效利用了原本无用的边,这反而简化了代码 #in ...
- 【2SAT+Trie】Gym101190B [NEERC2016] Binary Code
[题目] Gym 给定一些二进制编码,每个编码至多有一个位置不知道是什么.问是否存在一种补全编码方式,使得没有任何一个编码是另一个编码的前缀. n , ∑ ∣ s ∣ ≤ 5 × 1 0 5 n,\s ...
- 字符串匹配数据结构 --Trie树 高效实现搜索词提示 / IDE自动补全
文章目录 1. 算法背景 2. Trie 树实现原理 2.1 Trie 树的构建 2.2 Trie树的查找 2.3 Trie树的遍历 2.4 Trie树的时间/空间复杂度 2.5 Trie 树 Vs ...
- 算法 | 动画+解析,轻松理解「Trie树」
Trie这个名字取自"retrieval",检索,因为Trie可以只用一个前缀便可以在一部字典中找到想要的单词. 虽然发音与「Tree」一致,但为了将这种 字典树 与 普通二叉树 ...
- leetcode前缀树java_LeetCode 实现 Trie (前缀树)
题目大意: 略. 分析: 字典树模板. 代码如下: 1 class Trie { 2 public: 3 int passed; // 记录经过这个节点的字符串数量 4 int ends; // 记录 ...
- Trie树合并 + SG函数 ---- BZOJ4730. Alice和Bob又在玩游戏(动态开点Trie 树上全局异或标记 + 合并 + 博弈论)
题目大题 题目大意: 解题思路: 首先我们对于子树u的SG函数为SG函数为SG函数为 ⨁是异或和\bigoplus是异或和⨁是异或和 SG[u]=mex{⨁w∈(w的父亲在u到v的路径上)SG[w]∣ ...
最新文章
- mysql 存储中文问题
- javascript的族家族史
- Android移动开发之【Android实战项目】漂亮Button样式
- centos 日志切割_centos 创建 logrotate 进行日志分割
- (jQuery,YUI)哪一个适合我?
- BigData:绘制2018年福布斯中国富豪榜人根据出生地绘制个人或其家族财富分布热点图
- httpsurlconnection 写不进去authorization值_23. 假设检验的时候为什么常写p lt; 0.05,而不写具体的p值?...
- 力软敏捷开发框架真正源码_敏捷真正使谁受益?
- jquery ui tabs
- 【干货】华为组织成长的动力机制.pdf(附下载链接)
- VB 获取计算机的分辨率大小
- [UOJ455][UER #8]雪灾与外卖——堆+模拟费用流
- JavaScript学习笔记 - 提升
- 汉化破解:Restorator辅助绕过注册码
- 使用Luyten工具反编译jar包
- 哈工大同义词词林扩展版-资源分享
- 微信小程序商城开发记录一之需求分析
- sqlmap之sql注入(一)
- 2022化工自动化控制仪表考试试题及答案
- 编写python程序半径为2.11的圆球的体积_医学生理学(山东大学)知到智慧树题目答案...