CF 566A Matching Names
CF 566A Matching Names
题目描述
给出n个名字和n个昵称,求一个名字和昵称的劈配方案,使得被劈配的名字和昵称的最长公共前缀长度的和最大。
1<=n<=100000
字符总数最多800000
输入格式
第一行n
后面n行名字
最后n行昵称
输出格式
第一行最大最长公共前缀长度和
后面n行每行两个整数分别为名字和昵称的编号代表劈配方案,有SPJ
这题好神仙。
本来一看匹配,费用流费用流,一看数据,萎了
考虑是匹配字符串,遂用一些字符串的算法。
要求总匹配长度最大,由贪心我们先令单次匹配较大的匹配,容易证得这样不会更差。
把字符串放到字典树上,根据种类进行编号。
然后在dfs时维护一个系统队列,每次把一个点为根的子树下面的点给统计好,再做上面的。
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N=800010;
struct node
{
int ch[26];
vector <pair <int,int > > per;
}t[N];
char c[N];
int tot,n;
void add(int typ,int num)
{
int len=strlen(c);
int now=0;
for(int i=0;i<len;i++)
{
if(!t[now].ch[c[i]-'a']) t[now].ch[c[i]-'a']=++tot;
now=t[now].ch[c[i]-'a'];
}
t[now].per.push_back(make_pair(num,typ));
}
int q[2][N],l[2],r[2],ans[2][N],cnt,sum;
void dfs(int now,int dep)
{
int tl[2];
tl[0]=l[0],tl[1]=l[1];
l[0]=r[0]+1;
l[1]=r[1]+1;
for(int i=0;i<26;i++)
if(t[now].ch[i])
dfs(t[now].ch[i],dep+1);
for(int i=0;i<t[now].per.size();i++)
{
int typ=t[now].per[i].second;
q[typ][++r[typ]]=t[now].per[i].first;
}
while(l[0]<=r[0]&&l[1]<=r[1])
{
ans[0][++cnt]=q[0][r[0]--];
ans[1][cnt]=q[1][r[1]--];
sum+=dep;
}
l[0]=tl[0];
l[1]=tl[1];
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",c);
add(0,i);
}
for(int i=1;i<=n;i++)
{
scanf("%s",c);
add(1,i);
}
dfs(0,0);
printf("%d\n",sum);
for(int i=1;i<=n;i++)
printf("%d %d\n",ans[0][i],ans[1][i]);
return 0;
}
2018.7.25
CF 566A Matching Names的更多相关文章
- [CF566A]Matching Names
[CF566A]Matching Names 题目大意: A组和B组各\(n(n\le10^5)\)个字符串\((\sum|S|\le8\times10^5)\),将它们两两匹配,使得每组两个字符串的 ...
- CF Fox And Names (拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 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 ...
随机推荐
- JS继承方法
1.原型链: 每个构造函数都有一个原型对象,且有一个指针指向该原型对象(prototype),原型对象都包含一个指向构造函数的指针(constructor),而实例都包含一个指向原型对象的内部指针(p ...
- Dubbo使用心得2
- 【python 2.7】python读取json数据存入MySQL
同上一篇,只是适配 CentOS+ python 2.7 #python 2.7 # -*- coding:utf-8 -*- __author__ = 'BH8ANK' import json im ...
- rz和sz上传下载文件
安装软件包 yum install lrzsz 上传文件,输入rz选择文件上传(可以按住shift键多选) # rz sz 下载文件到本地,选择保存文件夹 # sz dd xshell设 ...
- 2017年4月8日Office 365 使用CSV文件导入邮件组
国内版 第一步: Import-Module msonline Connect-MsolService 输入用户名密码 第二步: Get-MsolUser" 第三步: Set-Executi ...
- ubuntu 设置全局代理
ubuntu配置shadowsocks全局代理 在mac.window平台下都有shadowsocks客户端,因此这两个平台不叙述太多,现在介绍ubuntu下的配置方法. 1.安装python lin ...
- Bracket Sequences Concatenation Problem括号序列拼接问题(栈+map+思维)
A bracket(括号) sequence is a string containing only characters "(" and ")".A regu ...
- Java 学习笔记 ------第五章 对象封装
本章学习目标: 了解封装的概念与实现 定义类.构造函数与方法 使用方法重载与不定长度自变量 了解static方法 一.Java封装概念 在面向对象程式设计方法中,封装(英语:Encapsulation ...
- vue cli3 配置postcss
1.安装postcss-import,postcss-cssnext 包 2.修改package.json 将postcss响应的内容替换为 "postcss": { " ...
- sql nolock是什么
百度:SQL Server 中的 NOLOCK 到底是什么意思? 文章地址:http://blog.sina.com.cn/s/blog_7d3b18a50100rfwg.html 查询语句加上 no ...