当前位置:网站首页>Cf566a greed + dictionary tree
Cf566a greed + dictionary tree
2022-07-25 15:34:00 【Brother Tazi is here】
The main idea of the topic :
Here are two piles of strings , Let you match them in pairs to make L C P LCP LCP And the biggest .
Topic ideas :
The first idea : First insert a bunch of strings into the dictionary tree , Then another pile of strings , One by one greedy query as long as possible LCP, Then delete .
That's not right , Because once you can't match all , Choosing which one to delete will have aftereffect .
Second thought : On top of that , First put the second pile LCP Delete after sorting .
This is obviously wrong , Delete other strings after LCP It's dynamic . It is also unable to dynamically maintain the string LCP.
Consider that each node in the dictionary tree stores strings containing this prefix . In this way, we can enumeration LCP
If we deal with it layer by layer from leaf nodes , Does it ensure that the processing is in order , From long to short .
let me put it another way : Before processing this node , Let its subtree nodes match first . The remaining unmatched strings can only be found in this LCP Match the .
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define fi first
#define se second
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
struct Node{
int nxt[26];
vector<int> q[2];
}tr[maxn * 8];
int cnt;
void add (string a , int t , int id){
int n = a.size();
int u = 0;
tr[u].q[t].push_back(id);
for (int i = 0 ; i < n ; i++){
int v = a[i] - 'a';
if (!tr[u].nxt[v]) tr[u].nxt[v] = ++cnt;
u = tr[u].nxt[v];
tr[u].q[t].push_back(id);
}
}
int match[2][maxn] , ans;
void dfs (int u , int step){
for (int i = 0 ; i < 26 ; i++)
if (tr[u].nxt[i])
dfs(tr[u].nxt[i] , step + 1);
vector<int> t[2];
for (int i = 0 ; i <= 1 ; i++)
for (auto & g : tr[u].q[i])
if (!match[i][g]) t[i].push_back(g);
int n = min(t[0].size() , t[1].size());
for (int i = 0 ; i < n ; i++) {
match[0][t[0][i]] = t[1][i];
match[1][t[1][i]] = t[0][i];
ans += step;
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
int n; cin >> n;
for (int i = 1 ; i <= n ; i++){
string a; cin >> a;
add(a , 0 , i);
}
for (int i = 1 ; i <= n ; i++){
string a; cin >> a;
add(a , 1 , i);
}
dfs(0 , 0);
cout << ans << endl;
for (int i = 1 ; i <= n ; i++){
cout << i << " " << match[0][i] << endl;
}
return 0;
}
边栏推荐
- 2021上海市赛-D-卡特兰数变种,dp
- Node learning
- 你准备好脱离“内卷化怪圈”了吗?
- UIDocumentInteractionController UIDocumentPickerViewController
- GAMES101复习:变换
- SQL cultivation manual from scratch - practical part
- Pat grade a 1151 LCA in a binary tree (30 points)
- ML - natural language processing - Basics
- JS URLEncode function
- matlab--CVX优化工具包安装
猜你喜欢

matlab--CVX优化工具包安装

ML - 自然语言处理 - 基础知识

MySQL installation and configuration super detailed tutorial and simple database and table building method

Pytorch学习笔记--常用函数总结3

为什么PrepareStatement性能更好更安全?

GAMES101复习:三维变换

Delayed loading source code analysis:

Phased summary of the research and development of the "library management system -" borrowing and returning "module

你准备好脱离“内卷化怪圈”了吗?

图论及概念
随机推荐
ML - natural language processing - Basics
Spark SQL common time functions
2016CCPC网络选拔赛C-换根dp好题
Binary complement
Local cache --ehcache
matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值
使用cpolar建立一个商业网站(如何购买域名)
数据系统分区设计 - 分区与二级索引
ML - natural language processing - Key Technologies
2021江苏省赛A. Array-线段树,维护值域,欧拉降幂
Args parameter parsing
Spark SQL null value, Nan judgment and processing
在网页上实现任意格式的音视频快速播放功能的开发总结。
分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
Pat grade a 1152 Google recruitment (20 points)
ML - Speech - advanced speech model
Singleton mode 3-- singleton mode
Spark SQL UDF function
JVM-参数配置详解
Spark AQE