fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Function to return the maximum number
  5. // of given operations required to
  6. // remove the given string entirely
  7. int find(string s)
  8. {
  9.  
  10. // If length of the string is zero
  11. if (s.length() == 0)
  12. return 0;
  13.  
  14. // Single operation can delete the entire string
  15. int c = 1;
  16.  
  17. // To store the prefix of the string
  18. // which is to be deleted
  19. string d = "";
  20.  
  21. for (int i = 0; i < s.length(); i++) {
  22.  
  23. // Prefix s[0..i]
  24. d += s[i];
  25.  
  26. // To store the substring s[i+1...2*i+1]
  27. string s2 = s.substr(i + 1, d.length());
  28.  
  29. // If the prefix s[0...i] can be deleted
  30. if (s2 == d) {
  31.  
  32. // 1 operation to remove the current prefix
  33. // and then recursively find the count of
  34. // operations for the substring s[i+1...n-1]
  35. c = 1 + find(s.substr(i + 1));
  36. break;
  37. }
  38. }
  39.  
  40. // Entire string has to be deleted
  41. return c;
  42. }
  43.  
  44. // Driver code
  45. int main()
  46. {
  47. string s = "aaaaaabbaabbaabbaabb";
  48.  
  49. cout << find(s);
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 4376KB
stdin
Standard input is empty
stdout
8