Codeforces Round #434 (Div. 2): D. Polycarp's phone book(字典树)
来源:互联网 发布:macbook怎么显示软件 编辑:程序博客网 时间:2024/06/04 19:09
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<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;typedef struct Trie_Node{int sum;Trie_Node *next[10];}Trie;char str[70005][12];char ans[12], temp[12];int bet;void Query(Trie *root, char *phone){int len;Trie *p = root;len = 0;for(;*phone!='\0';phone++){p = p->next[*phone-'0'];temp[++len] = *phone;if(p->sum==0){if(len<bet){bet = len;temp[len+1] = 0;memcpy(ans, temp, sizeof(ans));}return;}}}void Reduce(Trie *root, char *phone){int i;Trie *p = root;for(;*phone!='\0';phone++){p = p->next[*phone-'0'];p->sum--;}}void Insert(Trie *root, char *phone);void Del(Trie *root);int main(void){int i, j, n;Trie *root = new Trie;root->sum = 0;for(i=0;i<=9;i++)root->next[i] = NULL;scanf("%d", &n);for(i=1;i<=n;i++)scanf("%s", str[i]+1);for(i=1;i<=n;i++){for(j=1;j<=9;j++)Insert(root, str[i]+j);}for(i=1;i<=n;i++){bet = 12;for(j=1;j<=9;j++)Reduce(root, str[i]+j);for(j=1;j<=9;j++)Query(root, str[i]+j);for(j=1;j<=9;j++)Insert(root, str[i]+j);puts(ans+1);}Del(root);return 0;}void Insert(Trie *root, char *phone){int i;Trie *p = root;for(;*phone!='\0';phone++){if(p->next[*phone-'0']==NULL){Trie *temp = new Trie;temp->sum = 0;for(i=0;i<=9;i++)temp->next[i] = NULL;p->next[*phone-'0'] = temp;}p = p->next[*phone-'0'];p->sum++;}}void Del(Trie *root){int i;for(i=0;i<=9;i++){if(root->next[i]!=NULL)Del(root->next[i]);}free(root);}
- Codeforces Round #434 (Div. 2): D. Polycarp's phone book(字典树)
- Codeforces Round #434 (Div. 2) D Polycarp's phone book(字符串,字典树)
- Codeforces Round #434 Polycarp's phone book (字典树)
- Codeforces Round #434 (Div. 2, )-字典树&好题&板子-Polycarp's phone book
- Codeforces Round #434 D. Polycarp's phone book (字典树)
- codeforces 858D Polycarp's phone book(字典树)
- Codeforces 861 D Polycarp's phone book(字典树模板)
- 858D Polycarp's phone book 字典树
- codeforce 858D Polycarp's phone book(字典树)
- CodeForces 858D Polycarp's phone book(Trie)
- Codeforces 861 D. Polycarp's phone book (trie)
- Polycarp's phone book
- CodeForces 858C Did you mean... 、 CodeForces 858D Polycarp's phone book!黑科技
- Codeforces Round #434 (Div. 2)-D(字典树)
- Codeforces Round #367 (Div. 2) D——Vasiliy's Multiset(异或字典树)
- Codeforces Round #367 (Div. 2) D Vasiliy's Multiset(字典树)
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(字典树模板)
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (字典树二进制)
- python网络爬虫-属性获取及Lambda表达式
- java和c#接口定义区别--学习笔记
- 集合中的并发
- 事件分发机制 图解
- 虚拟机安装centos7
- Codeforces Round #434 (Div. 2): D. Polycarp's phone book(字典树)
- 异步任务HttpUrlconnect 的GET 网络请求
- 二叉搜索树
- 后台接收前台 String json 取单个值问题-java
- css选择器中:first-child与:first-of-type的区别
- 技术方案模板
- 数据库操作的作业练习
- PostgreSQL、Greenplum 日常监控 和 维护任务
- 自定义控件(2)