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的更多相关文章

  1. 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 ...

  2. codeforces#1257 F. Make Them Similar ( 经典中间相遇问题 )

    题目链接: http://codeforces.com/contest/1257/problem/F 题意: 给出$n$个30位整数 找到一个数,让它与这$n$个数分别异或,得到的$n$个数二进制1的 ...

  3. Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )

    图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...

  4. Educational Codeforces Round 76 (Rated for Div. 2)F - Make Them Similar

    题意: 给你n个数字(<230),求出一个数字使得所有数字^这个数字之后,二进制状态下的1的个数相同. 解析: 因为最大数字二进制数有30位,所以分为前15位和后15位,分别枚举0-1<& ...

  5. CodeForces 527B Error Correct System

    Error Correct System Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  6. CodeForces 148B Escape

    Escape Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  7. 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 ...

  8. CodeForces 173A Rock-Paper-Scissors 数学

    Rock-Paper-Scissors 题目连接: http://codeforces.com/problemset/problem/173/A Description Nikephoros and ...

  9. 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 ...

随机推荐

  1. android 与 小米1S刷机学习

    本文内容为本博客作者原创,转载请注明出处或者发私信. [名词] 1.ROM包 :安卓手机系统,以.ZIP结尾,类似windows的 win7系统包,300M-700M不止 2.卡刷(Recovery模 ...

  2. # centos7下FFmpeg环境部署记录

    # centos7下FFmpeg环境部署记录 随着视频在网站上的应用越来越多,越来越多的网站服务器需要支持视频转码,视频压缩,FFmpeg是目前最好用的网站服务器后台转码程序,应用最多.FFmpeg是 ...

  3. 【BZOJ 1592】[Usaco2008 Feb]Making the Grade 路面修整 dp优化之转移变状态

    我们感性可证离散(不离散没法做),于是我们就有了状态转移的思路(我们只考虑单不减另一个同理),f[i][j]到了第i块高度为j的最小话费,于是我们就可以发现f[i][j]=Min(f[i-1][k]) ...

  4. 论文讨论&&思考《Deformable Convolutional Networks》

    这篇论文真是让我又爱又恨,可以说是我看过的最认真也是最多次的几篇paper之一了,首先deformable conv的思想我觉得非常好,通过end-to-end的思想来做这件事也是极其的make se ...

  5. poj 1523 割点 tarjan

    Description Consider the two networks shown below. Assuming that data moves around these networks on ...

  6. rpm的使用:查询、安装、卸载、升级

    RPM 有五种操作模式,分别为:安装.卸载.升级.查询和验证. RPM 安装操作 命令: rpm -i 需要安装的包文件名 举例如下: rpm -i example.rpm 安装 example.rp ...

  7. Windows2008 – Task Scheduler – ‘Action “C:\Windows\SYSTEM32\cmd.exe” with return code 1’

    Remediation Edit Task Let us make the necessary changes, which is to specify the Start folder. Here ...

  8. ActiveMQ(4) ActiveMQ JDBC 持久化 Mysql 数据库

    ActiveMQ 消息持久化机制: ActiveMQ 消息的持久化机制有 JDBC.AMQ.KahaDB 和 LevelDB,其中本示例版本(5.15.2)默认机制为 KahaDB.无论哪种持久化机制 ...

  9. Python 数据库连接池DButils

    常规的数据库链接存在的问题: 场景一: 缺点:每次请求反复创建数据库连接,连接数太多 import pymysql def index(): conn = pymysql.connect() curs ...

  10. postman发送post请求,报405 Method Not Allowed

    postman发送post请求,报405 Method Not Allowed: 亲测有效的方案一: 检查请求header是否缺少必要信息.如果不清可以把所有的头部信息全部粘贴到header中,尝试是 ...