Codeforces Round #434 (Div.2)
来源:互联网 发布:xquartz2.7.5 mac 编辑:程序博客网 时间:2024/06/10 00:47
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.
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.
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.
3123456789100000000100123456
900001
4123456789193456789134567819934567891
21938191
题意:
有n个长度为9的电话号码,找出每个号码的只属于它们的最短子号码。
题解:
字典树,注意末尾。
#include<iostream>#include<string.h>#include<stdio.h>#include<vector>using namespace std;int n,head[15];char num[70000+10][15];struct node{ char num;int cnt,be; vector<node*>next; node() { num = cnt = be = 0; next.clear(); }}*tree,*add;int insert_(node *now,int x,int len,int pos){ if(x==len) return now->cnt; int si = (int) now->next.size(); for(int i=0;i<si;i++) { if(now->next[i]->num!=num[pos][x]) continue; if(now->next[i]->be!=pos) now->next[i]->be = pos,now->next[i]->cnt++; return insert_(now->next[i],x+1,len,pos); } node *add = new node; add->be = pos; add->num = num[pos][x]; add->cnt = 1; now->next.push_back(add); return insert_(add,x+1,len,pos);}string find_(node *now,int x,int len,int pos){ string ans = ""; if(x==len) return ""; int si = (int) now->next.size(); for(int i=0;i<si;i++) { if(now->next[i]->num!=num[pos][x]) continue; if(now->next[i]->cnt==1) { string s = ""; s+=num[pos][x]; return s; } ans += num[pos][x]+find_(now->next[i],x+1,len,pos); if(x==len-1) return "0000000000"; return ans; } return ans;}int main(){ tree = new node; scanf("%d",&n); memset(head,-1,sizeof head); for(int i=1;i<=n;i++) { scanf("%s",num[i]); for(int j=0;j<9;j++) { insert_(tree, j, 9, i); } } for(int i=1;i<=n;i++) { string s = ""; int len = 111; for(int j=0;j<9;j++) { string ss = find_(tree, j, 9, i); if(ss.size()<len) { s = ss; len = ss.size(); } } cout<<s<<endl; } return 0;}
- Codeforces Round #434 (Div. 2)
- Codeforces Round #434 (Div. 2)
- Codeforces Round #434 (Div.2)
- Codeforces Round #434 (Div. 2)
- Codeforces Round #434 (Div.2)
- Codeforces Round #434 (Div. 2)
- Codeforces Round #102 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #104 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #107 (Div. 2)
- Codeforces Round #108 (Div. 2)
- Codeforces Round #110 (Div. 2)
- Codeforces Round #122 (Div. 2)
- Codeforces Round #121 (Div. 2)
- Codeforces Round #124 (Div. 2)
- JS中的map、reduce
- JSTL 标签大全详解
- 1178: “顺”序列
- MyEclipse2015安装SVN插件
- android 多线程实现方式、并发与同步学习总结
- Codeforces Round #434 (Div.2)
- 东秦图灵杯.平方和和立方和·
- 搜狐2018校招笔试题
- 使用OpenCV训练Haar like+Adaboost分类器的常见问题
- 【NOIP2017提高A组模拟9.14】生命之树 (dsu on tree+trie)
- 1266:寻找幸运数
- tensorflow之session用法简述
- UVA 796 Critical Links(Tarjan求割边)
- 一个简单的51操作系统