943,562 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1325
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 10th, 2008
0

Palindrome re-visited

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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...
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
shankhs is offline Offline
58 posts
since Jan 2008
Dec 11th, 2008
0

Re: Palindrome re-visited

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
shankhs is offline Offline
58 posts
since Jan 2008
Dec 11th, 2008
0

Re: Palindrome re-visited

Looks like that was his PHD thesis , you may not be able to understand it at all.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Dec 11th, 2008
0

Re: Palindrome re-visited

Can i get another algorithm???
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
shankhs is offline Offline
58 posts
since Jan 2008
Dec 11th, 2008
0

Re: Palindrome re-visited

From the link you posted:
Quote ...
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

Quote ...
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.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 11th, 2008
0

Re: Palindrome re-visited

This is a simple algorithm..You can learn from this..
c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 11th, 2008
0

Re: Palindrome re-visited

Or...
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 12th, 2008
0

Re: Palindrome re-visited

Click to Expand / Collapse  Quote originally posted by mrboolf ...
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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
shankhs is offline Offline
58 posts
since Jan 2008
Dec 12th, 2008
0

Re: Palindrome re-visited

Click to Expand / Collapse  Quote originally posted by cikara21 ...
Or...
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
shankhs is offline Offline
58 posts
since Jan 2008
Dec 12th, 2008
0

Re: Palindrome re-visited

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:
cpp Syntax (Toggle Plain Text)
  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
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to Find The user input ?
Next Thread in C++ Forum Timeline: Get File creation Date





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC