codeforces 858d(字典树)
来源:互联网 发布:word mac版 编辑:程序博客网 时间:2024/05/18 18:14
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
题意:给出很多个号码,然后要你对每一个号码找出一个长度最短的识别码,即长度最短的同时,只有那一个电话包含这个识别码。
思路:利用字典树,把每一个号码的后缀都加入字典树中,但是为了避免自身有重复的子串而导致重复计数,可以在字典树的每个节点都加一个时间戳,记录上一次更新是哪个号码更新的,如果是本号码,就不在这个节点的计数中+1,否则就+1。
#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;int lenth[70005];char ans[70005][15];#define MAXNUM 10//定义字典树结构体typedef struct Trie{ int flag;//从根到此是否为一个单词 int t; //时间戳 Trie *next[MAXNUM];}Trie;//声明一个根Trie *root;//初始化该根void init(){ root = (Trie *)malloc(sizeof(Trie)); root->flag=0; root->t=-1; for(int i=0;i<MAXNUM;i++) root->next[i]=NULL;}//对该字典树的插入单词操作void insert(char *words,int x){ Trie *tem = root; char*word=words; while(*word!='\0') { if(tem->next[*word-'0']==NULL) { Trie *cur = (Trie *)malloc(sizeof(Trie)); for(int i=0;i<MAXNUM;i++) cur->next[i]=NULL; cur->flag=1; cur->t=x; tem->next[*word-'0']=cur; } tem = tem->next[*word-'0']; if(tem->t!=x) { tem->flag++; tem->t=x; } word++; }}//查询一个单词的操作void search(char *word,int x){ Trie *tem = root; int len=1; for(int i=0;word[i]!='\0';i++) { tem=tem->next[word[i]-'0']; if(tem->flag==1) { if(lenth[x]==-1||lenth[x]>len) { lenth[x]=len; strncpy(ans[x],word,len); return;}} len++; }}//释放字典树内存操作,由于本题测试数据后程序自动跳出,所以这里没写释放内存函数void del(Trie *cur){ for(int i=0;i<MAXNUM;i++) { if(cur->next[i]!=NULL) del(cur->next[i]); } free(cur);}int main(){char phone[70005][12];int n;while(~scanf("%d",&n)){init();memset(phone,0,sizeof(phone));memset(ans,0,sizeof(ans));memset(lenth,-1,sizeof(lenth));for(int i=0;i<n;i++){scanf("%s",phone[i]);for(int k=0;k<9;k++){insert(phone[i]+k,i);}}for(int i=0;i<n;i++){for(int j=0;j<strlen(phone[i]);j++){search(phone[i]+j,i);}}for(int i=0;i<n;i++){printf("%s\n",ans[i]);}del(root);}}
- codeforces 858d(字典树)
- codeforces 706D(字典树)
- codeforces 706D(字典树)
- codeforces 706D (字典树)
- codeforces 858D Polycarp's phone book(字典树)
- Codeforces Round #434 (Div. 2)-D(字典树)
- Codeforces 706d 字典树+贪心
- CodeForces 456D Elections(字典树+dp)
- CodeForces 456D Elections(字典树+dp)
- Codeforces 601D Acyclic Organic Compounds(dfs+字典树合并)
- Codeforces 706D Vasiliy's Multiset(异或字典树)
- Codeforces Round #367 (Div. 2) D——Vasiliy's Multiset(异或字典树)
- Codeforces Round #367 (Div. 2) D Vasiliy's Multiset(字典树)
- codeforces 706D. Vasiliy's Multiset 带删除操作的字典树(真模版)
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(字典树模板)
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (字典树二进制)
- Codeforces 706D Vasiliy's Multiset(异或字典树)
- Codeforces Round #434 D. Polycarp's phone book (字典树)
- java中equals方法的用法以及==的用法(转)
- Feature_Selection
- 第九周项目三——利用二叉树遍历思想解决问题
- Python代码Linux
- 计算后缀表达式、Java
- codeforces 858d(字典树)
- 用SQL实现 excel常用操作
- Edit Distance 解题报告
- Java数据类型和运算符
- c++中vector(向量)的用法
- 百度地图之显示地图
- JPanel面板的综合运用
- 第一次月总结!
- Hadoop常用命令