I have been writing a palindrome finder in C++ and I have succeeded in writing one that is.... basic to say the least.
I am looking simply to increase the runspeed of the program, right now it takes about ~1m 5s to run a test for palindromes / 2 word palindromes on a 1500 word wordlist using the functions that I have. I would like to try running it on a much larger file but fail to see where I can optimize further?
Any help would be appreciated: P.S. This is not for school, just for leisure.
#include <iostream>
#include <ostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
bool isPal(string);
int main() {
vector<string> sVec;
vector<string> sWords;
vector<string> sTwoWords1;
vector<string> sTwoWords2;
char myfile[256]="/home/Damien/Test.txt";
ifstream fin;
string str;
fin.open(myfile);
if(!fin){
cout << "fin failed";
return 0;
}
while(fin){
fin >> str;
sWords.push_back(str);
if(!fin){
break;
}
if(isPal(str)){
sVec.push_back(str);
}
else{
getline(fin, str);
}
}
reverse(sVec.begin(), sVec.end());
for(int i =0; i < sVec.size(); i++){
cout << sVec[i] << " is a Palindrome " <<endl;
}
// Test 2
for(int i=0; i<sWords.size(); i++){
for(int j=(i+1); j<sWords.size(); j++){
str = sWords[i]+sWords[j];
if(isPal(str)){
sTwoWords1.push_back(sWords[i]);
sTwoWords2.push_back(sWords[j]);
}
}
}
fin.close();
for(int i=0; i<sTwoWords1.size(); i++){
cout << sTwoWords1[i] << " and " << sTwoWords2[i] << " are palindromic. \n";
}
return 0;
}
bool isPal(string& testing) {
return std::equal(testing.begin(), testing.begin() + testing.size() / 2, testing.rbegin());
}
You're doing a lot of unnecessary work to test if it is a palindrome. Just use std::equal
:
#include <algorithm>
bool isPal(const string& testing) {
return std::equal(testing.begin(), testing.begin() + testing.size() / 2, testing.rbegin());
}
This will iterate from the beginning of the string to the middle and from the end of the string to the middle and compare the characters as it goes. I can't remember who showed me this, but I didn't think of it.
Edit: I learned it from Cubbi in another question about palindromes.
So i did some testing. In your approach Test2 takes long time.
Data: 2000 random 20 chars strings.
Your solution: 2500 ms. Seth Carnegie's: 500 ms.
Though i believe you have to multiply those by 2, because s+v can be palindrome while v+s isnt.
Idea: suppose we have a word abcd. Other words then can be palyndromes with this one are only cba and dcba. Lets check if we have those present.
...
#include <set>
using namespace std;
bool isPal(const string&);
int main() {
...
set<string> rWords;
...
while(fin){
fin >> str;
sWords.push_back(str);
if(!fin){
break;
}
reverse(str.begin(), str.end());//add reversed str to rWords
rWords.insert(str);
reverse(str.begin(), str.end());
...
}
...
// Test 2
for(int i = 0; i<sWords.size(); ++i)
for(int l = 0; l<sWords[i].length(); ++l)
if(isPal(sWords[i].substr(sWords[i].length()-l)))
{
string s = sWords[i].substr(0,sWords[i].length()-l);
set<string>::iterator it = rWords.find(sWords[i].substr(0,sWords[i].length()-l));
if(it != rWords.end())
{
string s = *it;
reverse(s.begin(), s.end());
if(s == sWords[i])//isPoly to itself
continue;
sTwoWords1.push_back(sWords[i]);
sTwoWords2.push_back(s);
}
}
...
return 0;
}
bool isPal(const string& testing) {
return std::equal(testing.begin(), testing.begin() + testing.size() / 2, testing.rbegin());
}
time: 15ms
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With