Page 1 of 3
11475 - Extend to Palindrome
Posted: Sun Aug 03, 2008 6:07 pm
by sonyckson
Hi!, its not the first time i see this problem, and its not the first time i dont see how to solve it... any hints? thanks! Eric.
Re: 11475 - Extend to Palindromes
Posted: Sun Aug 03, 2008 6:23 pm
by hamedv
Yes, i don't know too
There were problems like this an i solved them using DP, because the input size was less than 1000 but in this problem it's so huge.
Re: 11475 - Extend to Palindromes
Posted: Sun Aug 03, 2008 6:29 pm
by Ivan
Hint: Rolling hash might be a viable option here.
Re: 11475 - Extend to Palindromes
Posted: Sun Aug 03, 2008 8:55 pm
by Leonid
Knuth-Morris-Pratt algorithm is an option to consider if you're trying to solve this task.
Re: 11475 - Extend to Palindromes
Posted: Mon Aug 04, 2008 10:03 am
by baodog
Hi,
I use base N representation mod M, reading forward and backward, starting from
the last symbol. I keep get WA. I have tried many cases.
Help is appreciated! Thanks.
Code: Select all
Accepted... silly index error. Thanks!
Re: 11475 - Extend to Palindromes
Posted: Mon Aug 04, 2008 10:52 am
by MAK
@baodog:
Your code prints an extra unprintable character at the end of each line on my machine. But it looks otherwise ok.
@hamedv:
I'm curious about the O(n^2) DP you mentioned. The simple brute force way of solving this problem is also O(n^2). Could you please post an outline of your idea?
Re: 11475 - Extend to Palindromes
Posted: Mon Aug 04, 2008 6:25 pm
by hamedv
MAK wrote:@baodog:
Your code prints an extra unprintable character at the end of each line on my machine. But it looks otherwise ok.
@hamedv:
I'm curious about the O(n^2) DP you mentioned. The simple brute force way of solving this problem is also O(n^2). Could you please post an outline of your idea?
I mean this:
http://www.comp.nus.edu.sg/~stevenha/pr ... Palindrome
Re: 11475 - Extend to Palindromes
Posted: Mon Aug 04, 2008 10:11 pm
by MAK
Thanks Hamedv. That is a much harder problem. The present problem asks that you only consider adding characters at the end of the string, which simplifies matters a lot.
Re: 11475 - Extend to Palindromes
Posted: Tue Aug 19, 2008 8:13 pm
by emotional blind
I think that is not that hard. because the length of the string is only 1000. So O(N^2) solution will be enough.
Re: 11475 - Extend to Palindromes
Posted: Sat Aug 23, 2008 10:43 pm
by MAK
IMHO the idea behind the solution (and not the implementation) of this problem is somewhat simpler that the one Hamedv referred to.
Of course, what I find find hard (or easy) can quite naturally seem easy (or hard) to someone else

.
Re: 11475 - Extend to Palindromes
Posted: Thu Oct 16, 2008 4:58 pm
by sijal
i solved this using kmp string matching algorithm in 0.020 seconds
Re: 11475 - Extend to Palindromes
Posted: Thu Feb 05, 2009 5:48 pm
by Sylpharion
Anybody can help me?
I've use the KMP and I'm sure that my program run on the largest testcases. And it works.. but I have a TLE on my code.
Code: Select all
#include<iostream>
#include<stack>
using namespace std;
char text[100001], pat[100001];
int f[100001];
stack<char> tempS;
bool isPalindrome(char* T)
{
int i;
for(i=0;i<strlen(T)/2;i++)
if(T[i]!=T[strlen(T)-1-i])
return false;
return true;
}
void reverse(char T[], char* P)
{
int i;
char temp;
for(i=0;i<strlen(T);i++)
P[strlen(T)-1-i]=T[i];
P[strlen(T)]='\0';
}
void fFunction(char*P)
{
int i,j,m;
i=1;
j=0;
m=strlen(P);
while(i<=m-1)
{
if(P[j]==P[i])
{
f[i]=j+1;
i++;
j++;
}
else if(j>0)
j=f[j-1];
else
{
f[i]=0;
i++;
}
}
}
void KMPMatch(char* T, char*P)
{
bool done=false;
int i,j,m,trace=0,k;
i=0;
j=0;
while(i<=strlen(T) && !done)
{
m=strlen(P);
if(P[j]==T[i])
{
if(j==m-1)
{
cout<<text;
while(!tempS.empty())
{
cout<<tempS.top();
tempS.pop();
}
cout<<endl;
done=true;
}
i++;
j++;
}
else if (j>0)
{
trace=j;
j=f[j-1];
for(k=0;k<trace-j;k++)
{
tempS.push(P[strlen(P)-1]);
P[strlen(P)-1]='\0';
}
}
else
{
i++;
tempS.push(P[strlen(P)-1]);
P[strlen(P)-1]='\0';
}
}
}
int main()
{
int i;
while(cin>>text)
{
if(isPalindrome(text))
cout<<text<<endl;
else
{
reverse(text,pat);
fFunction(pat);
KMPMatch(text,pat);
}
}
return 0;
}
Re: 11475 - Extend to Palindromes
Posted: Wed Apr 01, 2009 2:26 pm
by MAK
Try an input with a large string of the same character with a different character near the end.
e.g.
xxxxxx......xxxy
xxxxxx........xyxx
Re: 11475 - Extend to Palindromes
Posted: Tue Apr 07, 2009 3:52 am
by Sylpharion
yes, it works very fast, but it was TLE again when i submitted...
oh.. help me...

Re: 11475 - Extend to Palindromes
Posted: Thu May 28, 2009 12:01 pm
by serur
Consider the dual of the problem: find the largest suffix of the input text that is a palindrome.
text = abB, where B is the reverse of b. Then textTEXT = abBbBA, then you find the longest tandem repeat...
This leads to O(nlogn) solution which gets TLE + MLE.