| | |
Palindrome re-visited
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 52
Reputation:
Solved Threads: 1
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
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...
Here is my code
C++ Syntax (Toggle Plain Text)
string palindrome(string str) { int n=sz(str); for(int l=n-1;l>=0;l--)//Palindrome can be of any size. { for(int i=0;i<n-l+1;i++) { int j=i+l-1; string str2=str.substr(i,j-i+1); if(check_pal(str2)) return str2;//check_pal simply checks whether //the string is palindrome or not. } } string t1=""; return t1; }
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...
Looks like that was his PHD thesis , you may not be able to understand it at all.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
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.
•
•
•
•
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.
•
•
•
•
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.
This is a simple algorithm..You can learn from this..
c++ Syntax (Toggle Plain Text)
#define MAX 128 bool check(const char *str, const char *ori) { return ((stricmp(str,ori)==0)?true:false); } std::string palindrome(const char *str) { int size=strlen(str); if(size>=MAX) return "failed"; char reverse_str[MAX]={NULL}; for(int i=0,j=((size)-1);i<size;i++,j--) reverse_str[i]=str[j]; if(check(reverse_str,str)) return reverse_str; return "it's not a palindrome"; } int main() { std::string m_str=palindrome("dlrow olleh hello world"); std::cout<<m_str; std::cin.get(); return 0; }
Last edited by cikara21; Dec 11th, 2008 at 9:20 am. Reason: strcmp to stricmp
Or...
c++ Syntax (Toggle Plain Text)
bool check(const std::string str, const std::string ori) { return ((stricmp(str.c_str(),ori.c_str())==0)?true:false); } std::string palindrome(const std::string str) { int size=str.length(); std::string reverse_str; for(int i=((size)-1);i>=0;i--) reverse_str.push_back(str[i]); if(check(reverse_str,str)) return reverse_str; return "it's not a palindrome"; } int main() { std::string m_str=palindrome("dlrow olleh hello world"); std::cout<<m_str.c_str(); std::cin.get(); return 0; }
•
•
Join Date: Jan 2008
Posts: 52
Reputation:
Solved Threads: 1
•
•
•
•
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.
But this is giving me TLE..I need to optimize it.
Last edited by shankhs; Dec 12th, 2008 at 1:04 am.
•
•
Join Date: Jan 2008
Posts: 52
Reputation:
Solved Threads: 1
•
•
•
•
Or...
c++ Syntax (Toggle Plain Text)
bool check(const std::string str, const std::string ori) { return ((stricmp(str.c_str(),ori.c_str())==0)?true:false); } std::string palindrome(const std::string str) { int size=str.length(); std::string reverse_str; for(int i=((size)-1);i>=0;i--) reverse_str.push_back(str[i]); if(check(reverse_str,str)) return reverse_str; return "it's not a palindrome"; } int main() { std::string m_str=palindrome("dlrow olleh hello world"); std::cout<<m_str.c_str(); std::cin.get(); return 0; }
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
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:
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.
Here's the code:
cpp Syntax (Toggle Plain Text)
// do some convolution string getLongestPalindrome(string fwd){ string rev = string(fwd.rbegin(), fwd.rend()); int sz = fwd.size(); int maxcount = 0, maxstart = 0; int start = 0, end = 0, count = 0; for(int overlap = 1; overlap <= 2*sz; overlap++){ count = 0; for(int i = 0; i < overlap; i++){ int index1 = sz - overlap + i; if(index1 >= 0 && index1 < fwd.size() && i < rev.size() && fwd[index1] == rev[i]){ if(!count){ start = i; end = i; } count++; } else{ if(count > maxcount){ maxcount = count; maxstart = start; } count = 0; } } } if(count > maxcount){ maxcount = count; maxstart = start; } return rev.substr(maxstart, maxcount); }
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: c++ find word from text file
- Next Thread: Get File creation Date
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






