Skip to content

Commit

Permalink
353 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
ksaveljev committed Apr 23, 2011
1 parent b070817 commit fba2320
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions 353.cpp
@@ -0,0 +1,38 @@
#include <iostream>
#include <string>
#include <set>
using namespace std;

set<string> palindromes;

bool is_palindrome(const string &input, int start, int end) {
string a(input.begin() + start, input.begin() + end);
string b(a.rbegin(), a.rend());

return a == b;
}

int count_palindromes(const string &input) {
int counter = 0;

for (int i = 0, sz = input.size(); i < sz; i++) {
for (int j = i + 1; j <= sz; j++) {
if (is_palindrome(input, i, j)) {
palindromes.insert(string(input.begin() + i, input.begin() + j));
}
}
}

return palindromes.size();
}

int main(void) {
string input;

while (cin >> input) {
palindromes.clear();
cout << "The string '" << input << "' contains " << count_palindromes(input) << " palindromes." << endl;
}

return 0;
}

0 comments on commit fba2320

Please sign in to comment.