看了一上午,并且和题解对拍了,感觉没啥问题,但是只过了第一个subtest,有输出为0的情况。
不排除是我写假了,但确实没拍出来。
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
inline int max(int x,int y){return x>y?x:y;}
inline int min(int x,int y){return x>y?y:x;}
const int M=3e6+5;
int n;
char s[M][31];
int cnt=0;
struct Trie{
int x[12],num,endd;
};Trie tree[M];
inline int getnum(char x)
{
if(x>='0'&&x<='9') return x-'0';
return 10;
}
inline void insert(int id)
{
int pos=0,len=strlen(s[id]+1),flag=0,res=0;
for(int i=1;i<=len;i++)
{
int num=getnum(s[id][i]);
if(!tree[pos].x[num]) tree[pos].x[num]=++cnt;
pos=tree[pos].x[num];
if(num==10) flag=1;
if(flag) tree[pos].num++;
}
tree[pos].endd++;
}
inline int find(int id)
{
int pos=0,len=strlen(s[id]+1),res=0;
for(int i=1;i<=len;i++)
{
int num=getnum(s[id][i]);
if(!tree[pos].x[num]) return 0;//其实肯定是不存在的
pos=tree[pos].x[num];
res+=tree[pos].num-tree[pos].endd;
}
return res+tree[pos].endd-tree[pos].num;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("xxx.out","r",stdin);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>s[i]+1;insert(i);
}
for(int i=1;i<=n;i++) cout<<find(i)<<"\n";
return 0;
}