#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
const int N = 500000+10;
typedef long long LL;
char str[N][11], a[11];
int ok[N*6], num[N*6], ch[N*6][11], sum[N*6];
int main()
{
int n, m=0;
scanf("%d", &n);
for(int i=0;i<n;i++)
{
scanf("%s",a);
int l=0;
for(int j=0;j<9;j++)
{
int k=0;
for(int p=j;p<9;p++)
{
int x=a[p]-'0';
if(!ch[k][x]) ch[k][x]=++m;
k=ch[k][x];
if(!ok[k]) num[l++]=k,ok[k]=1, sum[k]++;
}
}
for(int j=0;j<l;j++) ok[num[j]]=0;
for(int j=0;j<9;j++) str[i][j]=a[j];
}
for(int i=0;i<n;i++)
{
int mint=11, s=0, t=9;
for(int j=0;j<9;j++)
{
int k=0;
for(int p=j;p<9;p++)
{
k=ch[k][str[i][p]-'0'];
if(sum[k]==1&&p-j+1<mint)
{
mint=p-j+1,s=j,t=p;
break;
}
}
}
for(int j=s;j<=t;j++) putchar(str[i][j]);
puts("");
}
return 0;
}
自己写的大暴力 时限4s 跑3.961s刚过去了。。。
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000+10;
typedef long long LL;
char str[70000+10][20];
string ss, sx;
unordered_map<string,int>q,qx;
int main()
{
int t;
scanf("%d", &t);
q.clear();
for(int i=0;i<t;i++)
{
scanf("%s",str[i]);
ss=str[i];
qx.clear();
for(int k=1;k<=9;k++)
{
for(int j=0;j+k-1<9;j++)
{
sx=ss.substr(j,k);
//cout<<sx<<endl;
if(!qx.count(sx)) qx[sx]=1,q[sx]+=1;
}
}
}
for(int i=0;i<t;i++)
{
ss=str[i];
int flag=0;
for(int k=1;k<=9;k++)
{
for(int j=0;j+k-1<9;j++)
{
sx=ss.substr(j,k);
//cout<<sx<<endl;
//cout<<q[sx]<<endl;
if(q[sx]==1)
{
for(int h=j;h<=j+k-1;h++) printf("%c",str[i][h]);
printf("\n");
flag=1;
break;
}
}
if(flag) break;
}
}
return 0;
}