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. 【题解】CQOI2015任务查询系统

    主席树,操作上面基本上是一样的.每一个时间节点一棵树,一个树上的每个节点代表一个优先级的节点.把开始和结束时间点离散,在每一棵树上进行修改.注意因为一个时间节点可能会有多个修改,但我们要保证都在同一棵 ...

  2. React context基本用法

    React的context就是一个全局变量,可以从根组件跨级别在React的组件中传递.React context的API有两个版本,React16.x之前的是老版本的context,之后的是新版本的 ...

  3. Ubuntu下安装LNMP之nginx的卸载

    我在安装Nginx时,是采用自己从网上down下自己需要的nginx版本进行编译安装的,如果使用过apt库来进行安装的话可以参考这篇文章:ubuntu中彻底删除nginx 假如是编译安装的童鞋,可以按 ...

  4. 安卓titlebar的组合控件使用

    http://blog.csdn.net/itachi85/article/details/51435187

  5. i=i+1与i+=1的区别及效率(Java)

    原博客地址 在做个java优化的PPT时,看到了i=i+1与i+=1的区别,在这之前还真没想到那么细. 1.x=x+1,x+=1及x++的效率哪个最高?为什么? x=x+1最低,因为它的执行如下. ( ...

  6. shell编程 if 注意事项

    read -n 1 -p "Let's go(y or n):" if [ "$REPLY"x = "y"x -o "$REPLY ...

  7. codefoeces 671 problem D

    D. Roads in Yusland standard output Mayor of Yusland just won the lottery and decided to spent money ...

  8. bzoj 4999: This Problem Is Too Simple!

    Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x&l ...

  9. 在Xcode5下实现4.5,4.6的效果

      https://www.evernote.com/shard/s227/sh/a575caee-d6a8-4f43-9037-145b9a6913ca/c9a2befa22ce7c3f547f58 ...

  10. HDU1003MAX SUM (动态规划求最大子序列的和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...