codeforces 861 D. Polycarp's phone book

https://codeforces.com/contest/861/problem/D
题意:
给出多个长度为10的字符串,用一个在其他字符串中未出现的连续子串表示该字符串

input
3
123456789
100000000
100123456
output
9
000
01

思路:因为考虑是连续子串,直接使用字典树插入每个字符串的后缀,然后每次寻找该串的答案时,将该串的后缀在字典树上删除,然后寻找到长度最短的不在字典树上存在的子串

#include<bits/stdc++.h>
#include<tr1/unordered_map>
#define fi first
#define se second
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;
 
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 0x3f3f3f3f;
const int N = 1e6 + 10;
const ll mod = 10007;
const int base=131;
tr1::unordered_map<ll,ll> mp;
 
ll n,m,id,x,y,k,q;
ll num[N];//单词出现次数
ll tree[N][10];//tree[i][j]表示节点i的第j个儿子的节点编号
ll tot;//总结点数
char s[N][10];
char ans[10];
char tmp[10];
void ins(char *str)
{
	int root=0;
	for(int i=0;str[i];i++)
	{
		int id=str[i]-'0';
		if(!tree[root][id]) tree[root][id]=++tot;
		num[tree[root][id]]++;    //每个节点数统计
		root=tree[root][id];
 
	}
	//num[root]++;//单词数统计
}
void del(char *str)
{
	int root=0;
	for(int i=0;str[i];i++)
	{
		int id=str[i]-'0';
		num[tree[root][id]]--;    //每个节点数减少
		root=tree[root][id];
 
	}
	//num[root]--;//单词数减少
}
int query(char *str)
{
	int root=0;
 
	for(int i=0;str[i];i++)
	{
		tmp[i]=str[i];
		//show2(i,tmp[i])
		int id=str[i]-'0';
		if(!num[tree[root][id]])//字典树上的数目为0
		{
			if(i+1<k)
			{
 
				k=i+1;
				tmp[i+1]='\0';
				for(int c=0;c<=k;c++) ans[c]=tmp[c];
				//show2(str,ans)
				break;
			}
		}
		root=tree[root][id];
	}
	//return num[root];
}
int main() {
 
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
 
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>s[i];
		for(int j=0;j<9;j++)
		{
			ins(s[i]+j);
		}
	}
	for(int i=1;i<=n;i++)
	{
		k=10;
		for(int j=0;j<9;j++)	del(s[i]+j);
		for(int j=0;j<9;j++)	query(s[i]+j);
		for(int j=0;j<9;j++)	ins(s[i]+j);
		cout<<ans<<endl;
 
	}
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值