Codeforces 861 D. Polycarp's phone book (trie)
来源:互联网 发布:方舟生存进化画面优化 编辑:程序博客网 时间:2024/06/06 01:07
Description
There are n phone numbers in Polycarp’s contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct.
There is the latest version of Berdroid OS installed on Polycarp’s phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp’s contacts: 123456789, 100000000 and 100123456, then:
if he enters 00 two numbers will show up: 100000000 and 100123456,
if he enters 123 two numbers will show up 123456789 and 100123456,
if he enters 01 there will be only one number 100123456.
For each of the phone numbers in Polycarp’s contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number.
Input
The first line contains single integer n (1 ≤ n ≤ 70000) — the total number of phone contacts in Polycarp’s contacts.
The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct.
Output
Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them.
Examples input
3123456789100000000100123456
Examples output
900001
题意
有
思路
既然每一个手机号码只有 9 位,那我们可以建立字典树存储其所有的后缀,
树中每一个节点记录访问过该节点串的标号集合,
然后通过一次
考虑逐一插入串的所有后缀复杂度为
另外一种类似的做法可以将字典树换为后缀自动机,每次插入时间复杂度为
AC 代码
#include<bits/stdc++.h>using namespace std;typedef __int64 LL;const int maxn = 1e6+10;int tot;struct node{ int next[20]; set<int> cnt; node() { memset(next,0,sizeof(next)); }} trie[maxn];void init(){ memset(trie,0,sizeof(trie)); trie[0].cnt = set<int>(); tot=0;}char str[20];void insert_str(char *s,int dep){ int len = strlen(s); int tmp = 0; trie[tmp].cnt.insert(dep); for(int i=0; i<len; i++) { if(!trie[tmp].next[s[i]-'0']) { trie[tmp].next[s[i]-'0']=++tot; trie[trie[tmp].next[s[i]-'0']].cnt = set<int>(); } tmp=trie[tmp].next[s[i]-'0']; trie[tmp].cnt.insert(dep); }}typedef pair<int,string> P;map<int,string> ans;void bfs(){ queue<P> sk; sk.push(P(0,"")); while(!sk.empty()) { P p = sk.front(); sk.pop(); int id = p.first; string path = p.second; if(id!=0&&trie[id].cnt.size()<=1) { if(trie[id].cnt.size()==1) { int sl = *trie[id].cnt.begin(); if(ans[sl]==""||ans[sl].length()>path.length()) ans[sl] = path; } } for(int i=0; i<10; i++) if(trie[id].next[i]) sk.push(P(trie[id].next[i],path + char('0'+i))); }}int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); init(); int n; cin>>n; for(int i=0; i<n; i++) { cin>>str; int len = strlen(str); for(int j=0; j<len; j++) insert_str(str+j,i); } bfs(); for(int i=0; i<n; i++) cout<<ans[i]<<endl; return 0;}
- Codeforces 861 D. Polycarp's phone book (trie)
- CodeForces 858D Polycarp's phone book(Trie)
- Codeforces 861 D Polycarp's phone book(字典树模板)
- codeforces 858D Polycarp's phone book(字典树)
- Codeforces Round #434 D. Polycarp's phone book (字典树)
- Codeforces Round #434 (Div. 2): D. Polycarp's phone book(字典树)
- Codeforces Round #434 Polycarp's phone book (字典树)
- Polycarp's phone book
- CodeForces 858C Did you mean... 、 CodeForces 858D Polycarp's phone book!黑科技
- Codeforces Round #434 (Div. 2) D Polycarp's phone book(字符串,字典树)
- 858D Polycarp's phone book 字典树
- codeforce 858D Polycarp's phone book(字典树)
- Codeforces858D Polycarp's phone book
- cf434 B. Polycarp's phone book
- Codeforces Round #434 (Div. 2, )-字典树&好题&板子-Polycarp's phone book
- Codeforces 706D-Vasiliy's Multiset(Trie树)
- codeforces 589C Polycarp's Masterpiece(分治 折半搜索)
- codeforces 589 C. Polycarp's Masterpiece
- QT问题记录之EnterEvent事件无效
- HDU5514 Frogs
- android开发笔记汇总(一)
- Java 课堂笔记 3
- 匿名内部类是什么?为什么其访问外部变量必须是finally的?
- Codeforces 861 D. Polycarp's phone book (trie)
- Lintcode137 Clone Graph solution 题解
- 隐藏/显示左frame栏
- 浅析web Storage
- Python 符号 自学总结(学习中)
- Find K Min
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 I. Barty's Computer(哈希||字典树)
- Centos安装redis
- poj3352 Road Construction 边双连通分量tarjan算法