poj 1816 trie树+dfs(强烈推荐,含有通配符的匹配)

这个题目花了我一晚上才调出来,其实也不难,就是在trie树上搜索给定的单词,但是麻烦的一点是考虑的情况非常多,比如模式串可能重复,或者*?连续出现,最开始都没考虑,程序敲出来直接提交一看WA,然后看discuss里面,说模式可能重复我才知道情况比我想的要复杂,重新该了下程序,还是WA,主要是没考虑到情况*后面忘了判断有无*或者?的情况,最后WA了好几次才A的,程序跑了235M,前30呢,哈哈,多亏了某某大牛的一组测试数据。我会在代码后面附上这组数据,希望后面做这个题得不要犯我这钟低级错误。

测试数据:

View Code
1000 100
z
dg
njcjb
ftwkmi
tips
tc
z
*
yg
n
?s
rhwo
mn
yyjm
kfdaqk
ofkku
bdbby
tdqfpw
nmug
ltwbda
*asgjw
*e*f?
ygokm
c
nhz
?
f
at
*ua
h
exflz
ll
whzxxc
v
thbw
krw
cx
p
t
x
muca
o
*
pwx
pbqh
efff
?r
s
ldsr
jpmd
qfvd
*
mz
wkcpy
wqe
?xc
l
jhdq
scaug
v
u
?metd
ei
?*nbqh
jrkx
c
?s
rw
?
oslbg
cw
mwwir
mh
t
ugg
q
dkfbq
s
xwm
bhykgc
yulc
rmy
lvm
sd
*pz*
yn
xje
jzdo
?fsmbh
dgq
bxhx
sulemy
zavniy
njeakb
qzlk
xl
?
vl
yzwsu
*
bpazl
*
ww
aj
ozlqgv
?zrg
whlkx
pu
j
qpqhli
zdcaz
*
yxa
ozdf
afcd
vnh
rzy
ikj
ooazqt
?rtqxf
jfsms
aevlnz
mn
*qbk
catz
*
?
id
sh
idx
?ky
rrht
?
kea
jzz
ynw
sj
kdjdk
jg
z
dpel
t
s
?gujv
fygxgh
oga
*vs
uo
howqj
foy
g
?
hw
wih
g
fu
*ju
?
v
k
*g
fihag
wmnib
mss
*
ip
wapbhx
swie
*p
jd
sk
lapb
wykem
ibtsv
sonqn
yghby
?e?i
yhertj
i
zcsgx
e
q
dj
vnq
elswq
g
i
idm
*i?
rxsox
*
ca
?*j
syht
kc
qt
u
urfdtg
ho
m
iesahj
gip
q
*gr
?ob*
odwp
e
kyj
fj
*nx
xf
ey
?azn
wa
bi
dyhd
lgjxd
bg
*rsc
zx
?ok
yq
u
?
zs
d
hxuqxa
f
xfpm
ahn
ajcglk
xulf
?eht
zx
*icvei
tjmoid
ova
e
vr
z
p
o
vbx
p
a
?bj
hlk
d
pmczh
fe
oo
c
?*hf
bsgwh
p
*hytm
mo
*eip
owefwr
p
vuwtjb
w
*qiqm
fvbun
zqkj
zeoh
?
q
tgs
?
eac
w
ij
fhd
eau
*jo
okwxn
e
?hv
ciyek
lxf
rip
jcgmj
o
?fdc
f
nw
nr
axxh
zmijch
l
*wl
kzk
ba
x
uqygjw
jsewig
rgs
akp
?
ns
txzd
vlaoq
jy
z
ykqs
ickqdq
qosxo
xty
b
yyicz
caei
?e
onc
ek
?*
prnc
veh
wivlc
fi
?u
ztlt
ui
?yf
dsrnwl
kc
qjgrq
bjwaik
*opn
f
tcxl
c
*gor
twkmoi
?**
ncuc
jf
?ln
findj
byihj
d
wey
z
q
*ecv
aea
tzvm
?
hrotv
abvqbm
v
ew
bui
jnhzu
gfcpdv
lorb
**
trlju
?
evfnav
inf
zxfjxf
?
elnfpc
ro
b
?e
khrart
jnc
rgzubc
anig
*i
?tegju
g
xf
qf
wlm
n
qonllp
ofsey
wjazx
?
aunzkh
uq
w
?fftb
?jbw*c
rlc
xwegw
zfsny
to
?x
syn
vryo
bqkok
?spq
tgpat
?hxmw
hzt
wr
qrs
?*m
lcham
?tr
ym
*zygdp
tx
jn
?f
mhe
*ctv
l
iofza
kqx
*t
qk
?
vz
f
gpm
xxur
rmfk
zj
zp
vy
pktao
*
mbul
m
r
?uret
bfve
qdpmf
zf
ppbt
dl
jihakd
j
n
*fjl
xt
a
*qgz
bi
yjwz
xf
*u*n
auuia
ejjyi
satqwb
nx
*dt
b
lakk
yt
modpot
c
ov
*qly
ekm
ojqx
khdt
eg
tbc
ka
w
holf
wfhk
ufx
qmb
il
v
l
*zqs
h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*
*zielj
abzpr
ozo
cox
?j
xlny
ys
y
?dwm
dgzm
eokivc
xycqt
sk
qdn
u
fjr
ug
?eg
ltc
yh
?
go
bqbdv
pirgy
xqx
khunla
walb
ayb
?u
wtkc
orysl
u
kcv
ofr
f
?fy
rlk
md
*y
wr
hjb
?
i
*cec
ng
*v*
hb
fpofz
adnj
af
sjpv
u
?er
qf
*u
r
gz
uu
zaryxu
rk
*a
*jwnt
wffoer
p
jai
zodie
cwyzsl
vr
?jif
xxf
cw
hvvkn
nrck
?r
jv
tndm
f
y
ga
fc
k
ilxpnf
zfp
asmfww
z
tkc
ia
cb
jpl
s
?f*p
l
*wq
cu
k
?ex
qmitcz
t
oclnac
du
kj
*dqy
gsswyl
onln
x
yi
?
liw
pqak
aeooqs
wlemng
c
i
*x
hu
zcn
g
*krvy
nukvmv
oiivah
rvyn
n
?sj
u
pmbrh
j
r
iexkdu
yiossf
b
vwk
wc
?
ja
mwx
xdofctieniiqh
lw
j
ktvoucsay
thcoszgjzkuaeweap
shpwogb
pslux
wntyemet
sshawoifjnqsu
qcw
qtwlat
nykzthewkpnlokci
zw
yqjjbtfvwxqlalst
zxigqiaxzvn
vulhybhmadldfenydscz
kbwmbnhkduwmzarzzxm
xnjrovsgrxsn
j
ekpoeqbblwrrprente
ncktiroyqkjoirsixiku
fxnvjpu
qpxkj
masrvbrcelbikbqcxjr
tv
tbrkofxzqgyabpzlql
jzv
tddtccsicvpm
c
lhxwvizr
yjxpdhozgvoroggabx
kvfmfzvhtjlnqzoibxl
ngjfqixz
okvtlsoqki
ysvcypwpwgaxxcapqg
cssrndjgocivagmeufr
zqdwzvry
zttzhzfsrphan
xex
d
hceajhafqs
u
vinisatzsodceqdyhnhc
dzbtok
rrwqpylanzmww
h
xqcyjhgzbb
njzjnphuomktgjk
yqogoukcwf
at
tdmxadpj
pd
yxcjykd
evcpobgujbqzdezs
tjwezj
wmgywpw
cyzzwymdwfywc
s
cpqlaheklmqzmgrkxqx
ifhzdgtvmlvmuqm
hbbbpmuavw
tanbhduhdhhw
aqdnrmvrgpbl
k
q
jg
xysixbacsiojd
ugfjrwitffidedhye
djqwfyzd
tng
rs
qfctuudiodliuwm
nkxqscgwnef
lswd
wpwasjxx
gpkvkupntarhc
lhekwocsvmlmcuijh
ktebueoowc
mnrqyrgsiwzyn
ycjlg
ejrnfathqplcurrpddr
nnpctufzf
rcccztstsi
vporzp
ztibpaanpycxiaimjz
tyypy
mvsffmfajf
jzzrjwsokeeaimwuol
hfyuo
h
xarstlolg
wycjrhcfugsckcunqx
ykqwlvnchdnm
nagfvati
gpqmbwgmfm
lowjhxwvb
phtfouq
knzhygbgx
kpxe
dpmkantqmxpffqjoft

代码:

#include <iostream>
#include
<algorithm>
#include
<stdio.h>
using namespace std;
const int N=100002;
const int sonNum=28;
struct nodeIdx{
int index;
nodeIdx
*link;
nodeIdx(
int i=-1,nodeIdx *l=NULL):index(i),link(l){}
};
struct trieNode{
nodeIdx
*pIdx;
int idNums;
trieNode
*son[sonNum];
};
nodeIdx sIndex[N];
trieNode node[N
*7],root;
char pattrn[7],words[30];
int cnt=0,idxCnt=0,num=0,wordLen;
bool used[N];
void insertP(char * p,int idx)
{
int i;
trieNode
*tmp=&root;
while('\0' != *p)
{
if(NULL == tmp->son[*p-'a'])
{
tmp
->son[*p-'a']=&node[cnt];
for(i=0;i<sonNum;i++)
node[cnt].son[i]
=NULL;
node[cnt].idNums
=0;
node[cnt].pIdx
=NULL;
cnt
++;
}
tmp
=tmp->son[*p-'a'];
p
++;
}
tmp
->idNums++;
if(1 < tmp->idNums)
{
sIndex[idxCnt].index
=idx;
sIndex[idxCnt].link
=tmp->pIdx;
tmp
->pIdx=&sIndex[idxCnt];
idxCnt
++;
}
else
{
tmp
->pIdx=&sIndex[idxCnt];
sIndex[idxCnt].index
=idx;
sIndex[idxCnt].link
=NULL;
idxCnt
++;
}
}
void query(trieNode* curNode,int curIndex)
{
if(curIndex == wordLen)
{
if(curNode->idNums>0)
{
nodeIdx
*tmp=curNode->pIdx;
while(NULL != tmp)
{
if(!used[tmp->index])
{
// ans[num++]=tmp->index;
num++;
used[tmp
->index]=true;
}
tmp
=tmp->link;
}
}
if((NULL != curNode->son[26]) && (0 < curNode->son[26]->idNums))
{
nodeIdx
*tmp=curNode->son[26]->pIdx;
while(NULL != tmp)
{
if(!used[tmp->index])
{
// ans[num++]=tmp->index;
num++;
used[tmp
->index]=true;
}
tmp
=tmp->link;
}
}
return;
}
if(NULL != curNode->son[words[curIndex]-'a'])
{
query(curNode
->son[words[curIndex]-'a'],curIndex+1);
}
if(NULL != curNode->son[26])
{
if(0 < curNode->son[26]->idNums)
{
nodeIdx
*tmp=curNode->son[26]->pIdx;
while(NULL != tmp)
{
if(!used[tmp->index])
{
// ans[num++]=tmp->index;
num++;
used[tmp
->index]=true;
}
tmp
=tmp->link;
}
}
trieNode
*tmp=curNode->son[26];
int pos=wordLen-1;
while(pos>=curIndex)
{
if(NULL != tmp->son[words[pos]-'a'])
{
query(tmp
->son[words[pos]-'a'],pos+1);
}
      //一开始忘了判断*后面是否是?或者*了,贡献了好几个WA啊,考虑不周全
if(NULL != tmp->son[27])
{
query(tmp
->son[27],pos+1);
}
if(NULL != tmp->son[26])
{
query(tmp
->son[26],pos+1);
}
pos
--;
}
}
if(NULL != curNode->son[27])
{
query(curNode
->son[27],curIndex+1);
}
}

int main()
{
int n,m,i,j,k;
scanf(
"%d%d",&n,&m);
root
=node[cnt];
for(i=0;i<sonNum;i++)
node[cnt].son[i]
=NULL;
node[cnt].idNums
=0;
node[cnt].pIdx
=NULL;
cnt
=1;
for(k=0;k<n;k++)
{
memset(pattrn,
'\0',sizeof(pattrn));
scanf(
"%s",pattrn);
j
=strlen(pattrn);
if('*' == pattrn[j-1])
{
//将末尾全是*的缩为一个,多了也没用
while('*' == pattrn[j-2])
{
pattrn[j
-1]='\0';
--j;
}
}
for(i=0;i<j;i++)
{
if('*' == pattrn[i])
pattrn[i]
='z'+1;
else if('?' == pattrn[i])
pattrn[i]
='z'+2;
}
insertP(pattrn,k);
}
for(i=0;i<m;i++)
{
memset(words,
'\0',sizeof(words));
// scanf("%s",words);
cin>>words;
num
=0;
memset(used,
0,sizeof(used));
wordLen
=strlen(words);
query(
&root,0);
if(num)
{
// qsort(ans,num,sizeof(int),cmp);
for(k=0;k<n;k++)
{
if(used[k])
printf(
"%d ",k);
}
printf(
"\n");
// printf("%d",ans[0]);
// for(k=1;k<num;k++)
// printf(" %d",ans[k]);
// printf("\n");
}
else
printf(
"Not match\n");
}
return 0;
}

你可能感兴趣的:(trie)