CF566A Matching Names
將字串的資訊放到trie樹以後,貪心地合併深度最深的姓名和筆名。若某深度中有一些姓名和筆名剩餘,那麼就將剩餘的部分上傳至深度減一的節點中去,企圖與深度減一的姓名和筆名進行合併。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=8e5+5;
int n;
ll ans;
int tot,ch[N][26];
vector<int>cnt[N][2];
string s;
struct node{int x,y;};
vector<node>anss;
void dfs(int u,int dep)
{
for (register int i=0; i<26; ++i)
{
int v=ch[u][i];
if (v)
{
dfs(v,dep+1);
for (register int j=0; j<cnt[v][0].size(); ++j) cnt[u][0].push_back(cnt[v][0][j]);
for (register int j=0; j<cnt[v][1].size(); ++j) cnt[u][1].push_back(cnt[v][1][j]);
}
}
int now=min(cnt[u][0].size(),cnt[u][1].size());
ans+=(ll)now*dep;
for (register int i=1; i<=now; ++i)
{
anss.push_back((node){cnt[u][0][cnt[u][0].size()-1],cnt[u][1][cnt[u][1].size()-1]});
cnt[u][0].pop_back(); cnt[u][1].pop_back();
}
}
int main(){
scanf("%d",&n);
for (register int i=1; i<=n; ++i)
{
cin>>s;
int len=s.length();
int now=0;
for (register int j=0; j<len; ++j)
{
int x=s[j]-'a';
if (!ch[now][x]) ch[now][x]=++tot;
now=ch[now][x];
}
cnt[now][0].push_back(i);
}
for (register int i=1; i<=n; ++i)
{
cin>>s;
int len=s.length();
int now=0;
for (register int j=0; j<len; ++j)
{
int x=s[j]-'a';
if (!ch[now][x]) ch[now][x]=++tot;
now=ch[now][x];
}
cnt[now][1].push_back(i);
}
dfs(0,0);
printf("%lld\n",ans);
for (register int i=0; i<anss.size(); ++i) printf("%d %d\n",anss[i].x,anss[i].y);
return 0;
}
相關文章
- SSL - SSLHandshakeException: No subject alternative names matching IP address foundException
- 7-Overview-namesView
- F - Perfect Matching on a Tree
- 7.2 FM Index MatchingIndex
- DNS: More than just namesDNS
- set names utf8;
- lower_case_table_names
- [leetcode] 1023. Camelcase MatchingLeetCode
- Leetcode 10 Regular Expression MatchingLeetCodeExpress
- The English names of various berries All In One
- Talk about the naming of spring bean namesSpringBean
- SSL - SSLHandshakeException: No subject alternative names presentException
- [ABC126F] XOR Matching 題解
- CF1948G MST with Matching 題解
- JSnaughty: Recovering Clear, Natural Identifiers from Obfuscated JS NamesJSIDE
- Codeforces 954I Yet Another String Matching Problem
- [20240325]FORCE_MATCHING_SIGNATURE與DML.txt
- Learning Semantic Concepts and Order for Image and Sentence Matching筆記筆記
- LeetCode - 解題筆記 - 10- Regular Expression MatchingLeetCode筆記Express
- INSTALL_FAILED_NO_MATCHING_ABIS終極解決方案AI
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of typeSpringFrameworkBeanException
- “getfacl: Removing leading '/' from absolute path names”解決辦法REM
- MySQL8.0的坑之lower_case_table_namesMySql
- XAMRAIN的INSTALL_FAILED_NO_MATCHING_ABIS錯誤處理AI
- PostgreSQL DBA(108) - pgAdmin(Don't do this:Don't use upper names)SQL
- IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokensExceptionHTTP
- meiqua / shape_based_matching---模板匹配速度過慢問題
- Could not find a version that satisfies the requirement py4j (from versions: ) No matching distributUIREM
- JDK安全證書一個錯誤訊息 No subject alternative names presentJDK
- 【java細節】Java程式碼忽略https證照:No subject alternative names presentJavaHTTP
- [20221207]為什麼FORCE_MATCHING_SIGNATURE不一致.txt
- Spring 異常關鍵字 no matching editors or conversion strategy found 解決方法Spring
- Oracle修改instance_name、db_name、db_unique_name、service_namesOracle
- pytest報錯Hint: make sure your test modules/packages have valid Python names.PackagePython
- 閱讀翻譯Hugging Face Community Computer Vision Course之Feature Matching (特徵匹配)Hugging FaceUnity特徵
- 論文閱讀-CORA: Adapting CLIP for Open-Vocabulary Detection with Region Prompting and Anchor Pre-MatchingAPT
- start-stop-daemon: matching on world-writable pidfile /var/run/redis/redis-server.pid is insecurefailedRedisServerAI
- Different AG groups have the exactly same group_id value if the group names are same and the ‘CLUSTER_TYPE = EXTERNAL/NONE’None