[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. Java基础 -- 复用类(组合和继承)

    复用类有两种实现方式. 在新的类中产生现有类的对象,由于新的类是由现有类的对象所组成,所以这种方法称之为组合. 采用继承实现. 一  组合语法 下面创建两个类WaterSource和Sprinkler ...

  2. Python操作文件-20181121

    Python操作文件 Python操作文件和其他语言一样,操作的过程无非是先定位找到文件.打开文件,然后对文件进行操作,操作完成后关闭文件即可. 文件操作方式:对文件进行操作,主要就是读.写的方式,p ...

  3. RSA Encrypting/Decrypting、RSA+AES Encrypting/Decrypting

    catalogue . CryptoAPI介绍 . RSA Encrypting/Decrypting File 1. CryptoAPI介绍 0x1: Cryptography Service Pr ...

  4. nginx第三方库安装以及连接memcache

    一.nginx第三方模块的安装 第三方模块查询地址:https://www.nginx.com/resources/wiki/modules/ 后来新出来一个nginx memcache增强版,有空可 ...

  5. Java之final关键字详解

    1. 修饰类 当用final去修饰一个类的时候,表示这个类不能被继承. 注意: a. 被final修饰的类,final类中的成员变量可以根据自己的实际需要设计为fianl. b. final类中的成员 ...

  6. python chrome的自定义启动

    # encoding:utf-8 """@version: python27@author: fafa@site: http://www.phpgao.com@softw ...

  7. H5——while循环,for循环

    [循环结构的步骤] * ① 声明循环变量 * ② 判断循环条件 * ③ 执行循环体(while的{}中的所有代码) * ④ 更改循环变量 * 然后执行② ③ ④   var n=1; // ① 声明循 ...

  8. C# RabbitMQ

    鉴于本人很懒,发现好的文章一般都直接copy,本文大体摘自:https://www.cnblogs.com/MuNet/p/8546192.html 1.引言 RabbitMQ——Rabbit Mes ...

  9. java.util.zip.ZipException: invalid entry size

    启动maven项目时报java.util.zip.ZipException: invalid entry size (expected 7612 but got 5955 bytes) 可能是mave ...

  10. SQL Server - 四种排序, ROW_NUMBER() /RANK() /DENSE_RANK() /ntile() over()

    >>>>英文版 (更简洁易懂)<<<< 转载自:https://dzone.com/articles/difference-between-rownum ...