Codeforces 861 D. Polycarp's phone book (trie)

网友投稿 654 2022-08-26

Codeforces 861 D. Polycarp's phone book (trie)

Codeforces 861 D. Polycarp's phone book (trie)

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

题意

有 n

思路

既然每一个手机号码只有 9 位,那我们可以建立字典树存储其所有的后缀,

树中每一个节点记录访问过该节点串的标号集合,

然后通过一次 bfs 找出标号集合大小为 1

考虑逐一插入串的所有后缀复杂度为 O(n2) ,其中 n=9 ,最大有 70000 次输入,因此总大小为 70000×92

另外一种类似的做法可以将字典树换为后缀自动机,每次插入时间复杂度为 O(n) ,总大小为 70000×9

AC 代码

#includeusing namespace std;typedef __int64 LL;const int maxn = 1e6+10;int tot;struct node{ int next[20]; set cnt; node() { memset(next,0,sizeof(next)); }} trie[maxn];void init(){ memset(trie,0,sizeof(trie)); trie[0]-t = set(); tot=0;}char str[20];void insert_str(char *s,int dep){ int len = strlen(s); int tmp = 0; trie[tmp]-t.insert(dep); for(int i=0; i(); } tmp=trie[tmp].next[s[i]-'0']; trie[tmp]-t.insert(dep); }}typedef pair P;map ans;void bfs(){ queue

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]-t.size()<=1) { if(trie[id]-t.size()==1) { int sl = *trie[id]-t.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>str; int len = strlen(str); for(int j=0; j

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:POJ 3368 Frequent values (RMQ)
下一篇:Codeforces 861 C. Did you mean... (模拟)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~