[CF566A]Matching Names
[CF566A]Matching Names
题目大意:
A组和B组各\(n(n\le10^5)\)个字符串\((\sum|S|\le8\times10^5)\),将它们两两匹配,使得每组两个字符串的LCP之和最大,输出最大值,并输出方案。
思路:
Trie上贪心,在深的结点能匹配则匹配。
源代码:
#include<set>
#include<cstdio>
#include<vector>
const int N=1e5+1,L=8e5+1;
int n,ans;
char s[L];
std::vector<std::pair<int,int> > v;
class Trie {
private:
std::set<int> set[L][2];
int ch[L][26],par[L],sz;
int idx(const char &c) const {
return c-'a';
}
void erase(int p,const int &x,const bool &t) {
while(p) {
set[p][t].erase(x);
p=par[p];
}
set[0][t].erase(x);
}
public:
void insert(char s[],const int &id,const bool &t) {
set[0][t].insert(id);
for(register int i=0,p=0;s[i];i++) {
const int c=idx(s[i]);
if(!ch[p][c]) {
ch[p][c]=++sz;
par[ch[p][c]]=p;
}
p=ch[p][c];
set[p][t].insert(id);
}
}
void solve(const int &p,const int &dep) {
for(register int i=0;i<26;i++) {
if(ch[p][i]) {
solve(ch[p][i],dep+1);
}
}
while(!set[p][0].empty()&&!set[p][1].empty()) {
ans+=dep;
const int x=*set[p][0].begin();
const int y=*set[p][1].begin();
v.push_back(std::make_pair(x,y));
erase(p,x,0);
erase(p,y,1);
}
}
};
Trie t;
int main() {
scanf("%d",&n);
for(register int i=1;i<=n;i++) {
scanf("%s",s);
t.insert(s,i,0);
}
for(register int i=1;i<=n;i++) {
scanf("%s",s);
t.insert(s,i,1);
}
t.solve(0,0);
printf("%d\n",ans);
for(register unsigned i=0;i<v.size();i++) {
printf("%d %d\n",v[i].first,v[i].second);
}
return 0;
}
[CF566A]Matching Names的更多相关文章
- CF 566A Matching Names
CF 566A Matching Names 题目描述 给出n个名字和n个昵称,求一个名字和昵称的劈配方案,使得被劈配的名字和昵称的最长公共前缀长度的和最大. 1<=n<=100000 字 ...
- aufomaper Queryable Extensions ProjectTo
When using an ORM such as NHibernate or Entity Framework with AutoMapper's standard Mapper.Map funct ...
- MyEclipse 中各种 libraries 的含义
MyEclipse 中各种 libraries 的含义 JRE System Library,Java EE 5 Libraries,Referenced Libraries这三个都是各 ...
- 为AM335x移植Linux内核主线代码(35)使用platform中的GPIO
http://www.eefocus.com/marianna/blog/15-02/310352_46e8f.html 使用GPIO,当然可以自己编写驱动,比如之前的第34节,也可以使用Kernel ...
- 转:db2 backup 及 restore
db2 backup 及 restore 2011-06-21 18:12:20| 分类: AIX |举报 |字号 订阅 两个问题: db2=>list applications db ...
- Python 在Visual studio 中做单元测试进行TDD开发
Unit Tests Steve Dower edited this page on 14 Jul · 3 revisions Pages 38 Home Azure Remote Debugging ...
- Myeclipse中隐藏jar包
在package explorer的右上角有一个向下的小三角 点击选择Filter 在打开的对话框中 第一个选框中打上对勾 文字框中填上 *.jar 然后点击OK就行了 多个隐藏内容之间用逗号隔开 如 ...
- Supported method argument types Spring MVC
Supported method argument types The following are the supported method arguments: Request or respons ...
- db2 backup export
备份命令: db2 backup db test to /db2data/ 监控备份进度: db2 list utilities show detail <-进度 检测备份文件的有效性: db2 ...
随机推荐
- bootstrap学习: 折叠插件和面板
bootstrap提供了面板排版工具和折叠插件,能够用来实现新闻列表.留言板.博客分块等: 1.折叠插件: <a data-toggle="collapse" data-ta ...
- ArrayList的实现及原理
ArrayList ArrayList是最常见以及每个Java开发者最熟悉的集合类了,顾名思义,ArrayList就是一个以数组形式实现的集合,以一张表格来看一下ArrayList里面有哪些基本的元素 ...
- SpringBoot系列: Pebble模板引擎
===============================Java 模板引擎选择===============================SpringBoot Starter项目向导中可选的J ...
- [物理学与PDEs]第3章第5节 一维磁流体力学方程组 5.1 一维磁流体力学方程组
1. 当磁流体力学方程组中的量只依赖于 $t$ 及一个空间变量时, 该方程组称为一维的. 2. 一维磁流体力学方程组 $$\beex \bea \cfrac{\p H_2}{\p t}& ...
- luogu P5287 [HNOI2019]JOJO
传送门 神™这题暴力能A,这出题人都没造那种我考场就想到的数据,难怪我的垃圾做法有分 先考虑没有撤销操作怎么做,因为每次插入一段一样的字符,所以我们可以把\(x\)个字符\(c\)定义为\(cx\), ...
- Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks(理解)
0 - 背景 R-CNN中检测步骤分成很多步骤,fast-RCNN便基于此进行改进,将region proposals的特征提取融合成共享卷积层问题,但是,fast-RCNN仍然采用了selectiv ...
- iTOP-开发板-MiniLinux-C程序调用shell命令
本文档介绍的是在 linux 系统环境下 linux-C 调用 shell 命令实验步骤,和文档压缩包一起的“iTOP-开发板-MiniLinux-SHELL_V1.0.zip”是 c 程序源码.Li ...
- 调试 - Chrome调试
调试 - Chrome调试 打开开发人员工具 Ctrl+Shift+i可以打开开发人员工具. 功能面板 NetWork功能面板 在当前页面打开调试工具,刷新页面后点击NetWork可以查看当前页面的H ...
- POJ 3347 Kadj Squares (计算几何)
题目: Description In this problem, you are given a sequence S1, S2, ..., Sn of squares of different si ...
- 再说C模块的编写(1)
[前言] 在<Lua“控制”C>中对Lua调用C函数做了初步的学习,而这篇才是重中之重,这篇文章会重点的总结C模块编写过程中遇到的一些问题,比如数组操作.字符串操作和C函数的状态保存等问题 ...