11404 - Palindromic Subsequence
Posted: Sun Feb 24, 2008 9:33 am
I used DP solution to solve this problem, but I got TLE. It probably take O(n^3) to fill matrix.
[deleted]
any suggestion??
[deleted]
any suggestion??
There is a fast O(n^2) dp method (space requirement is also O(n^2) ) to determine the length of the longest palindrom, it's well known. But in this problem you have to print the lexicographically smallest palindrom, which is the longest, and you can do that in O(|size of alphabet|*n) time. (So the total time is O(n^2))ssangbuja wrote: any suggestion??
Thanks Robert I got AC.Robert Gerbicz wrote: So the total time is O(n^2)
There is no special case (but make sure that the word can be very long!).w k wrote:I'm getting WA using DP method. Are there any special inputs for this problem?
Wojciech
Code: Select all
h
hh
hy
yh
bab
abb
bba
abba
baba
abc
pu
bard
puma
abca
wyzw
wwww
book
Code: Select all
h
hh
h
h
bab
bb
bb
abba
aba
a
p
a
a
aba
wyw
wwww
oo
Code: Select all
dj
inc
klje
f
tgsmb
camd
wuqrgoa
mnl
byjfwdy
fvzzgke
rqe
mvwkohogfsx
cuhpqfxiunxo
fnwfghlxlg
gwxbp
ptutmv
ixdzjgqulmofs
adooewvkzskhdshhb
ltr
vllszwcfmg
idqqbqjertwskxan
n
sthucm
oduartneikjrdwokcenazl
miymgoxfaikovni
olfjkahkhwepcoyrhsaeed
ykbpchdphhvtqjjqijzmfpbinxhr
mwpvynkyxvwz
httxnbkqg
yyusciwqurzxwqopscyfaorv
oturynskpb
pkgoxdyddjau
itylavuwslcgjqccaglzfifhiynudssgb
fjpnhawxcftjnrrrflpwfou
evxmcftyfvhpahl
zpdpurzruypsihwqk
qdqkvyeybyrjx
bokenrlnlrcsgbczi
fnajtziuuiasselcov
gmpiqbsrkwsffrlytak
knprzizjinmhaoesymmhzregsxudbanvwd
ksefaihytncrtqhwvhytpzxhh
grriqcxmtepetanjpiqvlsyyermepwcwdeluxzwx
fjyxnicmnppxwkntjdhyh
ndkkjzbaouql
siuhfovapcwpedsmxknkspsdueazeucocf
oguodmhctxglulbneieqdgdotczmpabtmbn
bngsoicogmbajkkinjukmzovvkrqpbcgwrulpzzfaafrolqz
bygsgvjjhwamsv
qxtalhgqdykgivewohltbkdfinmrtma
lczdrsfargkjgb
clzcqcxvrtdifzvbkjhhm
qeptxcuyppyydtpkefxsbhkteggmkmtqpnnjvgfxxcpesgjns
bhfczpzyawklrnaoarzshujggfpjqaxi
pqrodtkwpfyycjphtijvdnjc
vzcofter
vxywaztuw
zbynpvykntuibgwmq
dnpvirovb
kczjrozzjkjvonvfzyxlxdxpmpcqqbxarcexvjau
bksuivw
tys
Can you explain how you print lexicographically least palindrome with O(|size of alphabet|*n) time? I thought hard, i am not able to get how to do it! Do you use any other array, other than the one used to compute the length of the longest palindrome?Robert Gerbicz wrote: But in this problem you have to print the lexicographically smallest palindrom, which is the longest, and you can do that in O(|size of alphabet|*n) time. (So the total time is O(n^2))
Code: Select all
pqrodtkwpfyycjphtijvdnjc
Code: Select all
dtpaaptd
Code: Select all
d
c
e
f
b
a
a
l
ydy
zz
e
oho
ufu
glxlg
b
tut
d
dkskd
l
ll
qbq
n
c
anekdkena
ioaoi
ahcha
bpqjjqpb
wvykyvw
tt
ycqrqcy
b
ddd
ilacccali
fpfrrrfpf
vftfv
purzrup
qdq
brlnlrb
aiuuia
kffk
nrzhmmhzrn
hthvhth
xepeyyepex
jxncnxj
kk
foaedsknksdeaof
mctgeiegtcm
gcomjkkjmocg
sjjs
alhgdghla
gjg
zcqcz
epcptkebektpcpe
fzraoarzf
dtpyyptd
c
waw
bnknb
viv
jvxxpmpxxvj
b
s
Could you please describe this algorithm? I couldn't find enough information on the net!rio wrote: This problem could also be solved by modified Hunt-Szymanski algorithm, an algorithm for LCS.
If the algorithm you claimed is not based on the fact that all LCS must be palindromic, then it is ok. The problem with LCS is that, there are some common subsequences which may not be palindromic. I found this thread, discussing on the topic.rio wrote: This problem could also be solved by modified Hunt-Szymanski algorithm, an algorithm for LCS.
In this case, you could get the lexicographically smallest palindrome by calculating the lexicographically smallest LCS.
Brief explanation of Hunt-Szymanski algorithm:Could you please describe this algorithm? I couldn't find enough information on the net!
Yes. The algorithm I'm talking is not simple adaption of LCS on s and reverse s.If the algorithm you claimed is not based on the fact that all LCS must be palindromic, then it is ok. The problem with LCS is that, there are some common subsequences which may not be palindromic.
Sorry for that! There was an error in the random generation. (some invalid spaces in the word).Anindya wrote:I am getting RTE,can any one help?
Robert,how for the input:u get:Code: Select all
pqrodtkwpfyycjphtijvdnjc
PLZ expain.I can't see any 'a' in the input string.Code: Select all
dtpaaptd
Does ur AC code produces this output?