Palindrome re-visited

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Palindrome re-visited

 
0
  #1
Dec 10th, 2008
I am searching an efficient algorithm to find all the palindromes in a string(actually I have to find the largest palindrome in a string)..
Here is my code
  1. string palindrome(string str)
  2. {
  3.  
  4. int n=sz(str);
  5.  
  6. for(int l=n-1;l>=0;l--)//Palindrome can be of any size.
  7. {
  8. for(int i=0;i<n-l+1;i++)
  9. {
  10. int j=i+l-1;
  11.  
  12. string str2=str.substr(i,j-i+1);
  13.  
  14. if(check_pal(str2)) return str2;//check_pal simply checks whether //the string is palindrome or not.
  15. }
  16. }
  17.  
  18. string t1="";
  19. return t1;
  20. }
This is an O(n^3)(since check_pal requires another loop),I searched and got this:
http://johanjeuring.blogspot.com/200...lindromes.html

But I cant understand a word of it What is his algo? How is it linear?
Please help me...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Re: Palindrome re-visited

 
0
  #2
Dec 11th, 2008
Hey guys Please help me...
I am new to algorithms if there is an algorithm to find longest palindrome in a string just give me a link thats enough I will try my best to understand that.
Thankyou.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,824
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Palindrome re-visited

 
0
  #3
Dec 11th, 2008
Looks like that was his PHD thesis , you may not be able to understand it at all.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Re: Palindrome re-visited

 
0
  #4
Dec 11th, 2008
Can i get another algorithm???
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Palindrome re-visited

 
0
  #5
Dec 11th, 2008
From the link you posted:
A naive algorithm for finding palindromes calculates all positions of an input array, and for each of those positions, calculates the length of the longest palindrome around that position.
That shouldn't be much difficult to implement, and

For each position, this function may take an amount of steps linear in the length of the array, so this is a quadratic-time algorithm.
Give it a try and then post your efforts in case you need help.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Palindrome re-visited

 
0
  #6
Dec 11th, 2008
This is a simple algorithm..You can learn from this..
  1. #define MAX 128
  2. bool check(const char *str, const char *ori)
  3. {
  4. return ((stricmp(str,ori)==0)?true:false);
  5. }
  6.  
  7. std::string palindrome(const char *str)
  8. {
  9. int size=strlen(str);
  10. if(size>=MAX)
  11. return "failed";
  12. char reverse_str[MAX]={NULL};
  13. for(int i=0,j=((size)-1);i<size;i++,j--)
  14. reverse_str[i]=str[j];
  15. if(check(reverse_str,str))
  16. return reverse_str;
  17. return "it's not a palindrome";
  18. }
  19.  
  20. int main()
  21. {
  22. std::string m_str=palindrome("dlrow olleh hello world");
  23. std::cout<<m_str;
  24. std::cin.get();
  25. return 0;
  26. }
Last edited by cikara21; Dec 11th, 2008 at 9:20 am. Reason: strcmp to stricmp
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Palindrome re-visited

 
0
  #7
Dec 11th, 2008
Or...
  1.  
  2. bool check(const std::string str, const std::string ori)
  3. {
  4. return ((stricmp(str.c_str(),ori.c_str())==0)?true:false);
  5. }
  6.  
  7. std::string palindrome(const std::string str)
  8. {
  9. int size=str.length();
  10. std::string reverse_str;
  11. for(int i=((size)-1);i>=0;i--)
  12. reverse_str.push_back(str[i]);
  13. if(check(reverse_str,str))
  14. return reverse_str;
  15. return "it's not a palindrome";
  16. }
  17.  
  18. int main()
  19. {
  20. std::string m_str=palindrome("dlrow olleh hello world");
  21. std::cout<<m_str.c_str();
  22. std::cin.get();
  23. return 0;
  24. }
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Re: Palindrome re-visited

 
0
  #8
Dec 12th, 2008
Originally Posted by mrboolf View Post
From the link you posted:


That shouldn't be much difficult to implement, and



Give it a try and then post your efforts in case you need help.
If you see the code in my first post I have done exactly that .the first loop determines the length of the substring to be checked and the second loop checks for each position in the array to get the substring of the specified length.
But this is giving me TLE..I need to optimize it.
Last edited by shankhs; Dec 12th, 2008 at 1:04 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Re: Palindrome re-visited

 
0
  #9
Dec 12th, 2008
Originally Posted by cikara21 View Post
Or...
  1.  
  2. bool check(const std::string str, const std::string ori)
  3. {
  4. return ((stricmp(str.c_str(),ori.c_str())==0)?true:false);
  5. }
  6.  
  7. std::string palindrome(const std::string str)
  8. {
  9. int size=str.length();
  10. std::string reverse_str;
  11. for(int i=((size)-1);i>=0;i--)
  12. reverse_str.push_back(str[i]);
  13. if(check(reverse_str,str))
  14. return reverse_str;
  15. return "it's not a palindrome";
  16. }
  17.  
  18. int main()
  19. {
  20. std::string m_str=palindrome("dlrow olleh hello world");
  21. std::cout<<m_str.c_str();
  22. std::cin.get();
  23. return 0;
  24. }
My problem is to find longest substring that is a palindrome not to check whether the string is palindrome or not.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Palindrome re-visited

 
0
  #10
Dec 12th, 2008
I have an dumb idea, what about an operation like convolving the test string with itself; i.e. doing a sliding comparision of the elements of the test string with the reversed-test-string. The longest consecutive string of matching elements is the longest palindrome.

Here's the code:
  1. // do some convolution
  2. string getLongestPalindrome(string fwd){
  3. string rev = string(fwd.rbegin(), fwd.rend());
  4. int sz = fwd.size();
  5. int maxcount = 0, maxstart = 0;
  6. int start = 0, end = 0, count = 0;
  7.  
  8. for(int overlap = 1; overlap <= 2*sz; overlap++){
  9. count = 0;
  10. for(int i = 0; i < overlap; i++){
  11. int index1 = sz - overlap + i;
  12. if(index1 >= 0 && index1 < fwd.size() && i < rev.size() && fwd[index1] == rev[i]){
  13. if(!count){
  14. start = i;
  15. end = i;
  16. }
  17.  
  18. count++;
  19. }
  20. else{
  21. if(count > maxcount){
  22. maxcount = count;
  23. maxstart = start;
  24. }
  25.  
  26. count = 0;
  27. }
  28. }
  29. }
  30.  
  31. if(count > maxcount){
  32. maxcount = count;
  33. maxstart = start;
  34. }
  35.  
  36. return rev.substr(maxstart, maxcount);
  37. }

Less complexity than your above snippet. Note that you don't actually have make a copy of the reverse string; I just did that because it was clearer in my head - you can just change the index on line 11/12 and point it into the fwd string.
Last edited by dougy83; Dec 12th, 2008 at 8:12 am. Reason: line numbers
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC