当前位置:网站首页>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;
}
边栏推荐
- ML - natural language processing - Introduction to natural language processing
- matlab randint,Matlab的randint函数用法「建议收藏」
- UIDocumentInteractionController UIDocumentPickerViewController
- 4PAM在高斯信道与瑞利信道下的基带仿真系统实验
- 看到很多App出现闪烁的图片,特别是会员页面
- 单例模式3--单例模式
- PAT甲级1152 Google Recruitment (20 分)
- Week303 of leetcode
- IOS interview questions
- GAMES101复习:变换
猜你喜欢

ML - 自然语言处理 - 关键技术

使用cpolar建立一个商业网站(如何购买域名)

Node learning

Solve the timeout of dbeaver SQL client connection Phoenix query

BPSK调制系统MATLAB仿真实现(1)

谷歌云盘如何关联Google Colab

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

ML - 语音 - 深度神经网络模型

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

《图书馆管理系统——“借书还书”模块》项目研发阶段性总结
随机推荐
ML - 自然语言处理 - 关键技术
wait()和sleep()的区别理解
Spark SQL UDF function
C # fine sorting knowledge points 9 Set 2 (recommended Collection)
Args parameter parsing
C#精挑整理知识要点12 异常处理(建议收藏)
Spark SQL common time functions
Xcode添加mobileprovision证书文件报错:Xcode encountered an error
The development summary of the function of fast playback of audio and video in any format on the web page.
JVM-动态字节码技术详解
Application of C language array in Sanzi chess -- prototype of Queen n problem
ML - natural language processing - Introduction to natural language processing
CF566A-贪心+字典树
BPSK调制系统MATLAB仿真实现(1)
Seata中jdbc下只有一个lib嘛?
Games101 review: 3D transformation
matlab--CVX优化工具包安装
UIDocumentInteractionController UIDocumentPickerViewController
HDU3873-有依赖的最短路(拓扑排序)
JS URLEncode function