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