[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 ...
随机推荐
- Day039--HTML
HTML小马哥博客 HTML CSS + DIV实现整体布局 1. HTML 超文本标记语言 对换行不敏感 空白折叠现象 标签要严格密封 新建HTML文件,输入 html:5,按tab键后,自动生成的 ...
- source insight如何删除没用的project 及其常见问题
4年09月05日 ⁄ 综合 ⁄ 共 439字 ⁄ 字号 小 中 大 ⁄ 评论关闭 我正在中文路径下加载了一个工程,结果一点击打开,source insight程序就会出现错误提示,要求关闭.我想可能是 ...
- 二.django项目环境搭建
Ⅰ.web框架介绍 1.socket 服务端 1)客户端(手机中各种app.浏览器)是用来与服务端(网站的服务器程序)进行交互的 2)服务端类似发电厂,客户端类似电器,socket类似插座,互联网的数 ...
- host-only局域网络
在网桥配置的情况下设置: 网桥配置链接 配置前: 配置后: vi /etc/sysconfig/network-scripts/ifcfg-eth0 这里是使用static静态配置,具体网络配置及声明 ...
- python之shelve模块详解
一.定义 Shelve是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的. 二.用途 可以作为一个简单的数据存储方案. 三.用法 使用时,只需要使用open函数获取一个 ...
- jenkins持续集成原理
转载: 原文地址:http://www.2cto.com/kf/201609/544550.html 持续集成 开发中,我们经常遇到一些奇怪问题,比如本地可以编译成功的代码但是同事们更新代码后编译出错 ...
- python去除html标签的几种方法
import re from bs4 import BeautifulSoup from lxml import etree html = '<p>你好</p><br/& ...
- 1.4分布式-通讯协议TCP/IP
服务器和浏览器的通讯依靠http协议,今天就来分析一下http协议的具体内容以及https的加密过程.除了这些协议,为了增加服务器和浏览器交互的可拓展性,也出现了rest风格的请求方式,方便调用接口. ...
- Servlet中转发和重定向的路径问题【转】
转发和重定向的路径问题 Servlet中有两种方式获得转发对象(RequestDispatcher):一种是通过HttpServletRequest的getRequestDispatcher()方法获 ...
- 树状数组BIT
模板1 #include<iostream> #include<cstdio> using namespace std; int n, m, c[500010]; inline ...