Codeforces Round #434 (Div. 2): 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 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:123456789100000000 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 from 1 to 9. 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 the i-th number from the contacts. If there are several such sequences, print any of them.

Examples
input
3
123456789
100000000
100123456
output
9
000
01
input
4
123456789
193456789
134567819
934567891
output
2
193
81
91

题意:

有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);
}


### 关于 UniApp 框架推荐资源与教程 #### 1. **Uniapp 官方文档** 官方文档是最权威的学习资料之一,涵盖了从基础概念到高级特性的全方位讲解。对于初学者来说,这是了解 UniApp 架构技术细节的最佳起点[^3]。 #### 2. **《Uniapp 从入门到精通:案例分析与最佳实践》** 该文章提供了系统的知识体系,帮助开发者掌握 Uniapp 的基础知识、实际应用以及开发过程中的最佳实践方法。它不仅适合新手快速上手,也能够为有经验的开发者提供深入的技术指导[^1]。 #### 3. **ThorUI-uniapp 开源项目教程** 这是一个专注于 UI 组件库设计实现的教学材料,基于 ThorUI 提供了一系列实用的功能模块。通过学习此开源项目的具体实现方式,可以更好地理解如何高效构建美观且一致的应用界面[^2]。 #### 4. **跨平台开发利器:UniApp 全面解析与实践指南** 这篇文章按照章节形式详细阐述了 UniApp 的各个方面,包括但不限于其工作原理、技术栈介绍、开发环境配置等内容,并附带丰富的实例演示来辅助说明理论知识点。 以下是几个重要的主题摘选: - **核心特性解析**:解释了跨端运行机制、底层架构组成及其主要功能特点。 - **开发实践指南**:给出了具体的页面编写样例代码,展示了不同设备间 API 调用的方法论。 - **性能优化建议**:针对启动时间缩短、图形绘制效率提升等方面提出了可行策略。 ```javascript // 示例代码片段展示条件编译语法 export default { methods: { showPlatform() { console.log(process.env.UNI_PLATFORM); // 输出当前平台名称 #ifdef APP-PLUS console.log('Running on App'); #endif #ifdef H5 console.log('Running on Web'); #endif } } } ``` #### 5. **其他补充资源** 除了上述提到的内容外,还有许多在线课程视频可供选择,比如 Bilibili 上的一些免费系列讲座;另外 GitHub GitCode 平台上也有不少优质的社区贡献作品值得借鉴研究。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值