fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. // Complete the substrCount function below.
  6. long substrCount(int n, string s) {
  7.  
  8. long int length_sub = 2;
  9. long int count = n;
  10. while(length_sub <= n){
  11. for(long int i = 0; i <= n - length_sub ; i++){
  12. string sub = s.substr(i, length_sub);
  13. //cout << sub << " ";
  14. string rev_sub(sub);
  15. reverse(rev_sub.begin(), rev_sub.end());
  16. //cout << rev_sub;;
  17. char c = sub[0];
  18. int flag = 0;
  19. for(long int j = 0; j < sub.length() / 2; j++){
  20. if(sub[j] != c || rev_sub[j] != c){
  21. flag = 1;
  22. break;
  23. }
  24. }
  25. if(flag == 0){
  26. //cout << " - Special\n";
  27. count++;
  28. }
  29. // else{
  30. // cout << "\n";
  31. // }
  32. }
  33. length_sub++;
  34. }
  35. return count;
  36.  
  37. }
  38.  
  39. int main()
  40. {
  41.  
  42. int n;
  43. cin >> n;
  44. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  45.  
  46. string s;
  47. getline(cin, s);
  48.  
  49. long result = substrCount(n, s);
  50.  
  51. cout << result << "\n";
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 15240KB
stdin
7
abcbaba
stdout
10