858D Polycarp's phone book 字典树

来源:互联网 发布:淘宝3c认证可以租吗 编辑:程序博客网 时间:2024/05/18 18:18
D. Polycarp's phone book
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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 from1 to9. 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 thei-th number from the contacts. If there are several such sequences, print any of them.

Examples
Input
3123456789100000000100123456
Output
900001
Input
4123456789193456789134567819934567891
Output
219381

91

暴力每个号码的所有字串 字典树查询

#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");}}



阅读全文
0 0
原创粉丝点击