10453 - Make Palindrome

All about problems in Volume CIV. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

10453 - Make Palindrome

Postby Tobbi » Thu Feb 27, 2003 1:20 am

I'm using a doubly linked list and look from the beginning and the end to put in the missing characters to make it a palindrome. Though I get the correct example inputs, I always get a WA from the judge. Could please someone with an AC post some further examples? Thanks a ton!
Tobbi
New poster
 
Posts: 17
Joined: Sat Jan 25, 2003 3:06 pm
Location: Europe

Postby Larry » Thu Feb 27, 2003 2:45 am

Code: Select all
abcd
aaaa
abc
aab
abababaabababa
pqrsabcdpqrs
aaab
acaaaba
zzzaaazz
pooq
aoob


Gives:
Code: Select all
3 abcdcba
0 aaaa
2 abcba
1 baab
0 abababaabababa
9 pqrsabcdpqrqpdcbasrqp
1 baaab
2 acbaaabca
1 zzzaaazzz
2 pqooqp
2 abooba


It's specially graded, so your actual palindrome may vary... it's a dynamic programming problem, so you should probably do it that way.. =)

(I had posted some 2000 character examples, but it's too long for this post.. maybe you should try those too..)
Larry
Guru
 
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City

Postby prom » Sun Mar 02, 2003 12:14 pm

this problem is very similiar to well known problem -- optimal matrix multiplication
prom
New poster
 
Posts: 10
Joined: Thu Jul 25, 2002 11:58 pm
Location: Poland

Postby Tobbi » Sun Mar 02, 2003 4:38 pm

Which problem do you mean exactly, Prom?

I suppose you mean some variation of optimal matrix chain multiplication that can be solved by dynamic programming...
Last edited by Tobbi on Sun Mar 02, 2003 5:54 pm, edited 1 time in total.
Tobbi
New poster
 
Posts: 17
Joined: Sat Jan 25, 2003 3:06 pm
Location: Europe

Postby prom » Sun Mar 02, 2003 5:08 pm

Consider a string x1x2x3...xn, s[1,n] is our optimal solution, then

s[m,n] = s[m+1,n-1], if xm=xn,
min(s[m+1,n],s[m,n-1])+1, otherwise

Maybe it is not exacly optimal matrix chain multiplication, but method of solving is similiar.

optimal matrix chain multiplication is O(n^3) .

Hope this helps.
prom
New poster
 
Posts: 10
Joined: Thu Jul 25, 2002 11:58 pm
Location: Poland

Postby Larry » Mon Mar 03, 2003 6:18 am

I did it using LCS, but maybe there's a faster solution..
Larry
Guru
 
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City

10453 - Make palindrome

Postby junjieliang » Sat May 03, 2003 2:05 pm

Solved... sorry :oops:
Last edited by junjieliang on Sun May 04, 2003 9:44 am, edited 1 time in total.
junjieliang
Experienced poster
 
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

Postby junjieliang » Sun May 04, 2003 9:42 am

Solved :oops:
Thanks anyway to all who read. And for all who have problems try this test: azbzczdzez
junjieliang
Experienced poster
 
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

10453

Postby Dmytro Chernysh » Mon May 05, 2003 7:17 pm

How did you find palidrome? Because it's easy to find the number, but to build palindrome is not so easy...
Please write a discription.
Dmytro Chernysh
Experienced poster
 
Posts: 146
Joined: Sat Apr 26, 2003 2:51 am

Postby Nak » Tue May 06, 2003 3:54 am

I used dynamic programming to solve
this. Let f(s) be the shortest
palindrome created from the string
s. There are two cases:

If the first and last character of
s is the same, say s=a[...]a, then
f(s) = af([...])a

Otherwise, say s=a[...]b, then
f(s) = af([...]b)a or bf(a[...])b,
whichever is shorter.

If f is memoized it is fast enough.
Hope this helps.
Nak
New poster
 
Posts: 14
Joined: Sat Oct 26, 2002 5:59 am
Location: Sweden

Thanks!

Postby Dmytro Chernysh » Tue May 06, 2003 4:04 am

Thanks Nak for reply!
But don't really undrestand the method :-(
Sure, dynamic programming should be applied, but...
Can you show more extact coding.
I'll be very thankful :-)
Dmytro Chernysh
Experienced poster
 
Posts: 146
Joined: Sat Apr 26, 2003 2:51 am

Postby Nak » Tue May 06, 2003 4:20 am

I think the easiest method is to
make a function which takes a
string and two indices as
arguments (start and end index of
a substring). Since these indices
will both be smaller than 1000 you
can make a memo-table which is
1000*1000 elements large and store
values from old function calls.
Then you pretty much just implement
the recursive "formula" in my
previous post. Of course you need
to stop when start>=end.

I don't want to post my working
code here, but if you like i can
mail it to you.
Nak
New poster
 
Posts: 14
Joined: Sat Oct 26, 2002 5:59 am
Location: Sweden

Postby anupam » Sat Jul 12, 2003 12:32 pm

Hey gyes, what's the answer??? :oops: :oops:
Plz help
"Everything should be made simple, but not always simpler"
anupam
A great helper
 
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm

Postby Larry » Sat Jul 12, 2003 3:19 pm

I get :

Code: Select all
5 azbzcezdzeczbza
Larry
Guru
 
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City

Postby anupam » Sat Jul 12, 2003 3:33 pm

will you please tell me the method(algorithm) you used. bcz i used a very inefficient alg i think and got wa.
but can't understand why.
--
Please describe your alg in short plz.
advanced thabnks to all.
"Everything should be made simple, but not always simpler"
anupam
A great helper
 
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm

Next

Return to Volume CIV

Who is online

Users browsing this forum: No registered users and 0 guests