[贪心+Trie] Codeforces #566A. Matching Names
来源:互联网 发布:淘宝设计教程 编辑:程序博客网 时间:2024/05/17 07:32
首先考虑直觉的贪心,每次找
考虑
接下来就是实现的问题了。容易想到
#include<cstdio>#include<iostream>#include<algorithm>#include<string>#include<vector>#include<cstring>using namespace std;const int maxn=100005;int n,ans,tot;string a[maxn],b[maxn];pair<int,int> res[maxn];struct node{ node* ch[26]; vector<int> V1,V2; node(){ memset(ch,0,sizeof(ch));V1.clear();V2.clear(); }} base[500000], *p_top=base, *root;typedef node* P_node;P_node newnode(){ return p_top++; }void Insert1(int id,string st){ P_node now=root; for(int i=0;i<st.size();i++){ if(!now->ch[st[i]-'a']) now->ch[st[i]-'a']=newnode(); now=now->ch[st[i]-'a']; } now->V1.push_back(id);}void Insert2(int id,string st){ P_node now=root; for(int i=0;i<st.size();i++){ if(!now->ch[st[i]-'a']) now->ch[st[i]-'a']=newnode(); now=now->ch[st[i]-'a']; } now->V2.push_back(id);}typedef node* P_node;void dfs(P_node p,int dep){ for(int i=0;i<=25;i++) if(p->ch[i]){ dfs(p->ch[i],dep+1); for(auto j : p->ch[i]->V1) p->V1.push_back(j); for(auto j : p->ch[i]->V2) p->V2.push_back(j); } while(p->V1.size()&&p->V2.size()){ ans+=dep; res[++tot]=make_pair(p->V1[p->V1.size()-1],p->V2[p->V2.size()-1]); p->V1.pop_back(); p->V2.pop_back(); }}int main(){ freopen("51nod1526.in","r",stdin); freopen("51nod1526.out","w",stdout); scanf("%d",&n); root=newnode(); for(int i=1;i<=n;i++) cin >> a[i], Insert1(i,a[i]); for(int i=1;i<=n;i++) cin >> b[i], Insert2(i,b[i]); dfs(root,0); printf("%d\n",ans); for(int i=1;i<=n;i++) printf("%d %d\n",res[i].first,res[i].second); return 0;}
阅读全文
0 0
- [贪心+Trie] Codeforces #566A. Matching Names
- codeforces 566A Matching Names
- 多对多LCP和最大 Trie DFS CodeForces - 566A Matching Names
- CodeForces 566 A Matching Names(Trie 匹配LCP和最大)
- CodeForces - 566A Matching Names(字典树)
- Matching Names(Trie树)-(VK Cup 2015 - Finals, online mirror)
- Codeforces 791 C. Bear and Different Names【贪心】
- (贪心)Codeforces Round #405 C. Bear and Different Names
- A. Dragons codeforces+贪心
- CodeForces 288A--贪心
- Codeforces 556A 贪心
- Codeforces Round #290 (Div. 1)A. Fox And Names
- Codeforces Round #290 (Div. 1) A. Fox And Names
- Codeforces 412A Poster(贪心)
- CodeForces 230A Dragons(贪心)
- CodeForces 230A 贪心+排序
- Codeforces 484A Bits(贪心)
- Codeforces 484A - Bits (贪心)
- 从开发者角度解析 Android N 新特性!
- Android O源码Settings之NFC与Tap&pay
- jquery 序列化表单对象
- 从java到python(变量)
- 云栖大会现场——除了达摩院这件大事_你知道VISLAB吗?
- [贪心+Trie] Codeforces #566A. Matching Names
- ThreadDump分析笔记(一) 解读堆栈
- JQuery与Echart方法tooltip冲突的解决
- MTK Camera驱动移植
- URLDecoder: Incomplete trailing escape (%) pattern错误处理
- mysql用merge引擎进行分表
- 最全收集整理GitHub上受欢迎的Android UI Library
- 什么是P问题、NP问题和NPC问题
- Leetcode 330. Patching Array