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

  1. CF 566A Matching Names

    CF 566A Matching Names 题目描述 给出n个名字和n个昵称,求一个名字和昵称的劈配方案,使得被劈配的名字和昵称的最长公共前缀长度的和最大. 1<=n<=100000 字 ...

  2. aufomaper Queryable Extensions ProjectTo

    When using an ORM such as NHibernate or Entity Framework with AutoMapper's standard Mapper.Map funct ...

  3. MyEclipse 中各种 libraries 的含义

    MyEclipse 中各种 libraries 的含义       JRE System Library,Java EE 5 Libraries,Referenced  Libraries这三个都是各 ...

  4. 为AM335x移植Linux内核主线代码(35)使用platform中的GPIO

    http://www.eefocus.com/marianna/blog/15-02/310352_46e8f.html 使用GPIO,当然可以自己编写驱动,比如之前的第34节,也可以使用Kernel ...

  5. 转:db2 backup 及 restore

    db2 backup 及 restore 2011-06-21 18:12:20|  分类: AIX |举报 |字号 订阅     两个问题: db2=>list applications db ...

  6. Python 在Visual studio 中做单元测试进行TDD开发

    Unit Tests Steve Dower edited this page on 14 Jul · 3 revisions Pages 38 Home Azure Remote Debugging ...

  7. Myeclipse中隐藏jar包

    在package explorer的右上角有一个向下的小三角 点击选择Filter 在打开的对话框中 第一个选框中打上对勾 文字框中填上 *.jar 然后点击OK就行了 多个隐藏内容之间用逗号隔开 如 ...

  8. Supported method argument types Spring MVC

    Supported method argument types The following are the supported method arguments: Request or respons ...

  9. db2 backup export

    备份命令: db2 backup db test to /db2data/ 监控备份进度: db2 list utilities show detail <-进度 检测备份文件的有效性: db2 ...

随机推荐

  1. Springboot 5.Springboot 返回cookies信息的post接口开发

    首先创建一个类,类里面首先登陆获取到cookie,然后带着cookie去发送请求 package com.course.server; import com.course.bean.User; imp ...

  2. FreeNAS系统总结

    FreeNAS简介 FreeNAS是一套免费的NAS服务器,它能将一部普通PC变成网络存储服务器.该软件基于FreeBSD,Samba 及PHP,支持CIFS (samba), FTP, NFS pr ...

  3. Breastcancer社区评论下载

    首页 某个社区 某社区的一个话题 目标:获取这个网站所有话题的所有评论相关信息 python实现 # -*- coding: utf-8 -*- """ @Datetim ...

  4. Docker下安装rabbitmq

    拉取镜像 docker pull rabbitmq:-management 启动镜像(默认用户名密码),默认guest 用户,密码也是 guest docker run -d --: -p : rab ...

  5. cas单点登录防止登出退出后刷新后退ticket失效报500错

    https://www.cnblogs.com/wangyang108/p/5844447.html

  6. [Kubernetes]如何让集群为我们工作?

    前一段时间倒腾k8s的时候,写了一系列的博客,有很多人不理解那样做的意义是什么,为什么要那样做,这篇文章就尝试解释一下,在实际环境中,是如何让集群为我们工作的. 因为只研究了一个月左右的时间,认识难免 ...

  7. iOS app内打开safari完成google的OAuth2认证

    最近使用google的oauth认证,发现不再允许使用UIWebview进行认证了,必须使用系统游览器,使用游览器也不一定要在app之间跳转,ios使用SFSafariViewController就可 ...

  8. iOS rebuild from bitcode对ipa大小的影响

    https://developer.apple.com/library/content/technotes/tn2432/_index.html 为了测试一下rebuild from bitcode的 ...

  9. Python 通用爬虫 和讯博客 scrapy

    目标站点需求分析 通用爬虫,获取和讯博客所有博文 涉及的库 scrapy,re,requests,mysql URL RULE 解析单页源码 保存到数据库 结果

  10. js在数组arr中随机获取count数量的元素

    // 在数组arr中随机获取count数量的元素; const getRandomArrayElements = (arr, num) => { // 新建一个数组,将传入的数组复制过来,用于运 ...