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.
3 123456789 100000000 100123456
9 000 01
4 123456789 193456789 134567819 934567891
2 193 81 91
也就是一定有解
解题思路:我们把这n个字符串的每一个字符串的后缀(每个字符串共10个后缀)都取出来,插入到一颗trie树上,这个这n个字符串一共10 * n个后缀都在这棵
trie树上。然后我们对树上的每个节点维护一个sum,sum表示到当前节点也就是一个字符串的某个后缀包含从根节点到该节点表示的字符串的字符串的个数。所以
我们对于一个字符串,我们只需要把这个字符串的所有后缀取出来,在这棵树上跑一边就行,跑的时候遇到sum = 1表示只有本字符串含有这个子串(从根节点到
当前节点),那么我们就找到了一个最短的,然后在所有后缀中取最优值就行。
#include <bits/stdc++.h> using namespace std; const int maxn = 700000; int n; int tot; int len; char st[70000 + 10][12]; struct node{ int sum; node *Next[11]; }Node[2 * maxn]; node *root; char ch[100]; char S[100]; map<string, int> mp; int Min; string ans; void init() { tot = 0; for(int i = 0; i < maxn; i++) { Node[i].sum = 0; memset(Node[i].Next, 0, sizeof(Node[i].Next)); } root = &Node[tot++]; } void Insert(char *s, int depth, node* p) { s[depth] = ch[depth]; s[depth + 1] = '\0'; string test = string(s); if(!mp[test]) { mp[test] = 1; p->sum++; } if(depth == len - 1) return; int num = ch[depth + 1] - '0'; if(p->Next[num]) Insert(s, depth + 1, p->Next[num]); else { node *newnode = &Node[tot++]; p->Next[num] = newnode; Insert(s, depth + 1, p->Next[num]); } } void solve(char *s, int depth, node* p) { s[depth] = ch[depth]; s[depth + 1] = '\0'; string test = string(s); if(p->sum == 1) { if(depth < Min) { Min = depth; ans = test; } return; } if(depth == len - 1) return; int num = ch[depth + 1] - '0'; solve(s, depth + 1, p->Next[num]); } int main() { while(~scanf("%d", &n)) { init(); char term[100]; for(int i = 1; i <= n; i++) { mp.clear(); scanf("%s", st[i]); int l = strlen(st[i]); for(int j = 0; j < l; j++) { int res = 0; for(int k = j; k < l; k++) { ch[res++] = st[i][k]; } ch[res] = '\0'; len = strlen(ch); char ss = ch[0]; if(root->Next[ss - '0']) Insert(S, 0, root->Next[ss - '0']); else { node *newnode = &Node[tot++]; root->Next[ss - '0'] = newnode; Insert(S, 0, root->Next[ss - '0']); } } } for(int i = 1; i <= n; i++) { Min = 11; int l = strlen(st[i]); for(int j = 0; j < l; j++) { int res = 0; for(int k = j; k < l; k++) { ch[res++] = st[i][k]; } ch[res] = '\0'; len = strlen(ch); solve(S, 0, root->Next[ch[0] - '0']); } cout<<ans<<endl; } } return 0; }