858D Polycarp's phone book 字典树
来源:互联网 发布:淘宝3c认证可以租吗 编辑:程序博客网 时间:2024/05/18 18:18
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from0. 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 from1 to9. 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 thei-th number from the contacts. If there are several such sequences, print any of them.
3123456789100000000100123456
900001
4123456789193456789134567819934567891
21938191
暴力每个号码的所有字串 字典树查询
#include<stdio.h>#include<string.h>#include<ctype.h>#include<algorithm>using namespace std;const int maxm = 10;typedef struct Trie{int num;Trie *next[maxm];Trie(){num = 1;memset(next, NULL, sizeof(next));}};char str[80000][10];struct node{int s, len;}stu;Trie *root = new Trie;void insert(int s,int Id){Trie *p = root, *q;for (int i = s;str[Id][i] != '\0';i++){int id = str[Id][i] - '0';if (p->next[id] == NULL){q = new Trie;p->next[id] = q;p = p->next[id];}else{p->next[id]->num++;p = p->next[id];}}}int find(int s,int Id){Trie *p = root;for (int i = s;str[Id][i] != '\0';i++){int id = str[Id][i] - '0';if (p->next[id] == NULL)return 0;p = p->next[id];if (p->num == 0){if (i - s < stu.len){stu.len = i - s;stu.s = s;return 0;}}}}void work_1(int s, int Id){Trie *p = root;for (int i = s;str[Id][i] != '\0';i++){int id = str[Id][i] - '0';if (p->next[id] == NULL)return;p = p->next[id];p->num--;}}void work_2(int s, int Id){Trie *p = root;for (int i = s;str[Id][i] != '\0';i++){int id = str[Id][i] - '0';if (p->next[id] == NULL)return;p = p->next[id];p->num++;}}int main(){int n, i, j, k, sum;root->num = 0;scanf("%d", &n);for (i = 1;i <= n;i++){scanf("%s", str[i]);for (j = 0;str[i][j] != '\0';j++)insert(j, i);}for (i = 1;i <= n;i++){stu.len = 1e9 + 7;for (j = 0;str[i][j] != '\0';j++)work_1(j, i);for (j = 0;str[i][j] != '\0';j++)find(j, i);for (j = 0;str[i][j] != '\0';j++)work_2(j, i);for (j = stu.s;j <= stu.s + stu.len;j++)printf("%c", str[i][j]);printf("\n");}}
- 858D Polycarp's phone book 字典树
- codeforce 858D 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 (Div. 2) D Polycarp's phone book(字符串,字典树)
- Codeforces 861 D Polycarp's phone book(字典树模板)
- Codeforces Round #434 Polycarp's phone book (字典树)
- CodeForces 858D Polycarp's phone book(Trie)
- Polycarp's phone book
- Codeforces 861 D. Polycarp's phone book (trie)
- Codeforces858D Polycarp's phone book
- Codeforces Round #434 (Div. 2, )-字典树&好题&板子-Polycarp's phone book
- CodeForces 858C Did you mean... 、 CodeForces 858D Polycarp's phone book!黑科技
- cf434 B. Polycarp's phone book
- ACM 字典树 Phone List & Hat’s Words
- 字典树phone list
- Phone List(字典树)
- 微信用户授权流程+代码
- spring+mvc get请求,post请求编码问题(utf-8)
- plsql登录进去的时候,有个弹窗,但是我勾选了,下次不再显示,但是我又想让他弹出了,怎么恢复?
- 程序员高逼格指南
- 解决64位系统IIS网站发布出现未能加载文件或程序集“...”或它的某一个依赖项。试图加载
- 858D Polycarp's phone book 字典树
- uva 10972 添加几条变使得无向图为双联通分量
- eclipse 编写mapreduce程序(wordCount)
- 2^N-2的N次方
- 编程范式 17 笔记 并行化
- django-apps下面有多个app以及增加搜索路径
- 欢迎使用CSDN-markdown编辑器
- 加载等待特效框架 MBProgressHUD
- jquery eval()解析JSON,ajax通过return返回数据,js读取解析JSON数据