Codeforces 856B - Similar Words
856B - Similar Words
题意
如果一个字符串可以通过去掉首位字母得到另一个字符串,则称两个字符串相似。
给出一个字符串集合,求一个新的字符串集合,满足新集合里的字符串是原字符串集合中的字符串的前缀且字符串两两不相似,问新集合里字符串的最大数量。
分析
注意到字符串都是基于原串的前缀,考虑用 \(Trie\) 优化,可以高效存储所有的前缀串。考虑相似的定义,一个较长串的相似串是可以唯一确定的(去掉首字母),通过这个关系,我们可以把两个字符串连边,最终我们可以构造出一棵树,考虑题目所求,那么我们实际上求得的是子串的集合大小减去树上的最小点覆盖集的大小,利用 树形DP 求解。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
vector<string> v;
vector<int> G[MAXN];
int nxt[MAXN][26], val[MAXN], vis[MAXN], dp[MAXN][2];
int root, L, cnt;
int newnode() {
memset(nxt[L], -1, sizeof nxt[L]);
return L++;
}
void init() {
L = 0;
root = newnode();
}
void clear() {
cnt = 0;
for(int i = 0; i < L; i++) {
val[i] = vis[i] = 0;
G[i].clear();
}
}
void insert(string S) {
int now = root;
for(int i = 0; i < S.length(); i++) {
int d = S[i] - 'a';
if(nxt[now][d] == -1) {
nxt[now][d] = newnode();
}
now = nxt[now][d];
if(!val[now]) cnt++;
val[now] = 1;
}
}
int query(string S) {
int now2 = nxt[root][S[0] - 'a'], now = root;
for(int i = 1; i < S.length(); i++) {
now = nxt[now][S[i] - 'a'];
now2 = nxt[now2][S[i] - 'a'];
if(now == -1) break;
if(!vis[now2] && val[now]) {
vis[now2] = 1;
G[now].push_back(now2);
G[now2].push_back(now);
}
}
}
void dfs(int fa, int u) {
vis[u] = 1;
dp[u][0] = 0; dp[u][1] = 1;
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(v != fa) {
dfs(u, v);
dp[u][1] += min(dp[v][0], dp[v][1]); // 将当前点加入点覆盖集中
dp[u][0] += dp[v][1]; // 当前点不加入点覆盖集
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--) {
v.clear();
init();
int n;
cin >> n;
for(int i = 0; i < n; i++) {
string s;
cin >> s;
v.push_back(s);
insert(s);
}
for(int i = 0; i < n; i++) {
query(v[i]);
}
int rt = 1;
for(int i = 0; i < L; i++) vis[i] = 0;
for(int i = 0; i < L; i++) {
if(!vis[i] && G[i].size() > 0) {
dfs(i, i);
cnt -= min(dp[i][0], dp[i][1]);
}
}
cout << cnt << endl;
clear();
}
return 0;
}
Codeforces 856B - Similar Words的更多相关文章
- Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]
题目链接:https://codeforces.com/contest/1090/problem/D Vasya had an array of n integers, each element of ...
- codeforces#1257 F. Make Them Similar ( 经典中间相遇问题 )
题目链接: http://codeforces.com/contest/1257/problem/F 题意: 给出$n$个30位整数 找到一个数,让它与这$n$个数分别异或,得到的$n$个数二进制1的 ...
- Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )
图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...
- Educational Codeforces Round 76 (Rated for Div. 2)F - Make Them Similar
题意: 给你n个数字(<230),求出一个数字使得所有数字^这个数字之后,二进制状态下的1的个数相同. 解析: 因为最大数字二进制数有30位,所以分为前15位和后15位,分别枚举0-1<& ...
- CodeForces 527B Error Correct System
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- CodeForces 148B Escape
Escape Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心
B. War of the Corporations 题目连接: http://www.codeforces.com/contest/625/problem/B Description A long ...
- CodeForces 173A Rock-Paper-Scissors 数学
Rock-Paper-Scissors 题目连接: http://codeforces.com/problemset/problem/173/A Description Nikephoros and ...
- Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题
B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
随机推荐
- CF115B Lawnmower
题目描述 You have a garden consisting entirely of grass and weeds. Your garden is described by an n×mn×m ...
- BZOJ3132 上帝造题的七分钟 【二维树状数组】
题目 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的 ...
- vector创建2维数组
以前我要建立一个二维数组,总是使用 int N=5, M=6; vector<vector<int> > Matrix(N); for(int i =0; i< Matr ...
- MySQL 数据库性能优化之缓存参数优化
在平时被问及最多的问题就是关于 MySQL 数据库性能优化方面的问题,所以最近打算写一个MySQL数据库性能优化方面的系列文章,希望对初中级 MySQL DBA 以及其他对 MySQL 性能优化感兴趣 ...
- Hyperledger Fabric架构详解
区块链开源实现HYPERLEDGER FABRIC架构详解 区块链开源实现HYPERLEDGER FABRIC架构详解 2018年5月26日 陶辉 Comments 10 Comments hyper ...
- hdu 3473 (划分树)2
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Sqlserver面试题
1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 ...
- HDFS 的Trash回收站
1)在core-site.xml文件中添加这个配置 在每个节点(不仅仅是主节点)上添加配置 core-site.xml,增加如下内容 <property> <name>fs.t ...
- 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过
题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...
- 【POJ 1719】 Shooting Contest (二分图匹配)
题目链接 把每一列能射的两行和这一列连边,然后跑一边匈牙利就行了. #include <cstdio> #include <cstring> #include <algo ...