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 ...
随机推荐
- Git 删除服务器的远程分支
git push origin :分支名 可能会出现,在A机子操作,刷新下成功删除,但在B机子上还显示,再用下命令提示不存在该分支,只要再推送一个任意分支即可正常显示
- BZOJ 3545 / 洛谷 P4197 Peaks 解题报告
P4197 Peaks 题目描述 在\(\text{Bytemountains}\)有\(N\)座山峰,每座山峰有他的高度\(h_i\).有些山峰之间有双向道路相连,共\(M\)条路径,每条路径有一个 ...
- js生成图片
var image = new Image(); var c = document.getElementById("myCanvas"); var ctx = c.getConte ...
- 一个 React & Redux的目录树
|-----------------------------------------| | | | React & Redux | | | |------------------------- ...
- Vue根据URL传参来控制全局 console.log 的开关
如果你的项目中console.log了很多信息,但是发到生产环境上又不想打印这些信息,这时候就需要设置一个全局变量,如:debug, 用正则匹配一下参数: const getQueryStr = (n ...
- 解决Vue方法中setTimeout改变变量的值无效
把data里的变量继承过来重新封装一下 let that = this; this.rightAnswer = false; setTimeout(function() { that.rightAns ...
- [hdu 2298] 物理推导+二分答案
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2298 #include<bits/stdc++.h> using namespace st ...
- 在eclipse中使用JUnit4,以及使用JUnit4进行单元测试的技巧
一 在eclipse中使用JUnit4 首先在工程上右键,选择属性,找到Java Builder Path,添加JUnit4的lib,如下图: 在要测试的类上右键新建 Junit test cas ...
- Python基础(4)_集合、布尔类型
一.集合 集合的作用一:关系运算集合的作用二:去重 定义集合:集合内的元素必须是唯一的:集合内的元素必须是可hash的,也是就不可变类型:集合是无序的 s={'egon',123,'egon','1' ...
- poj1379 Run Away
传送门:http://poj.org/problem?id=1379 [题解] 题目大意:求(0,0)->(X,Y)内的一个点,使得这个点到给定的n个点的最小距离最大. 模拟退火 一开始可以先把 ...