Advertisement
juanjo12x

UVA_353_Pasky_Palindromes

Aug 13th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <string>
  6. #include <cctype>
  7. #include <stack>
  8. #include <queue>
  9. #include <list>
  10. #include <vector>
  11. #include <map>
  12. #include <set>
  13. #include <sstream>
  14. #include <stdlib.h>
  15. #include <cmath>
  16. #define FOR(i,A) for(typeof (A).begin() i = (A).begin() ; i != (A).end() ; i++)
  17. #define mp make_pair
  18. #define debug( x ) cout << #x << " = " << x << endl
  19. #define clr(v,x) memset( v, x , sizeof v )
  20. #define all(x) (x).begin() , (x).end()
  21. #define rall(x) (x).rbegin() , (x).rend()
  22. #define TAM 110
  23.  
  24. using namespace std;
  25.  
  26. typedef pair<int,int> ii ;
  27. typedef long long ll ;
  28. typedef long double ld ;
  29. typedef pair<int,ii> pii ;
  30.  
  31. string cad;
  32. set<string>mySet;
  33.  
  34. bool isPalindrome(string str){
  35. string tmp = str;
  36. reverse(tmp.begin(),tmp.end());
  37. if(str == tmp)
  38.    return true;
  39. return false;
  40. }
  41.  
  42. void generateCombination(int strWord, int index){
  43. string strTmp = ""; //cout<<index<<endl;
  44.    for(int x=index; x<cad.size(); x++)
  45.    {
  46.         strTmp += cad[x];
  47.         if(strTmp.size() == strWord)
  48.         {
  49.             if(isPalindrome(strTmp))
  50.                mySet.insert(strTmp);
  51.             strTmp = "";
  52.             if(index<cad.size()-1)
  53.                generateCombination(strWord,++index);
  54.  
  55.             return;
  56.  
  57.         }
  58.         if(index == cad.size()-1 || x == cad.size()-1)
  59.            return;
  60.    }
  61. }
  62.  
  63. void getLetter(){
  64. string letter = "";
  65.     for(int x=0; x<cad.size(); x++)
  66.     {
  67.         letter += cad[x];
  68.         mySet.insert(letter);
  69.         letter = "";
  70.     }
  71. }
  72.  
  73. int main(){
  74. int wordSize;
  75. int initPos;
  76.  
  77. while(cin>>cad){
  78.  
  79.     if(cad.size() == 1)
  80.        printf("The string \'%s\' contains 1 palindromes.\n",cad.c_str());
  81.     else{
  82.          wordSize = 2;
  83.          initPos = 0;         while(wordSize<=cad.size()){
  84.             generateCombination(wordSize, initPos);
  85.             wordSize++;
  86.          }
  87.          getLetter();
  88.          printf("The string \'%s\' contains %d palindromes.\n",cad.c_str(), mySet.size());
  89.          mySet.clear();
  90.         }
  91. }
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement