fork download
  1. #include <iostream>
  2. #include<string.h>
  3. using namespace std;
  4.  
  5.  
  6. int length_last(string str)
  7. {
  8. int count = 0;
  9. bool flag = false;
  10. for (int i = str.length() - 1; i >= 0; i--) {
  11. if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
  12. flag = true;
  13. count++;
  14. }
  15. else {
  16. if (flag == true)
  17. return count;
  18. }
  19. }
  20. return count;
  21. }
  22.  
  23. int solve(string s1, string s2, char c, int i){
  24. string temp = s1;
  25. s1 = s1.substr(i, s1.length());
  26. if(c == 'Y'){
  27. size_t f1 = s1.find(s2 + " ");
  28. if (f1 != string::npos && i+f1 == 0)
  29. return i+f1;
  30. size_t f2 = s1.find(" " + s2 + " ");
  31. if (f2 != string::npos)
  32. return i+f2+1;
  33. size_t f3 = s1.find_last_of(" " + s2);
  34. // cout<<i+f3;
  35. // cout<<temp.length();
  36. if (f3 != string::npos)
  37. return (i+f3)-length_last(temp)+1;
  38. }
  39. else {
  40. size_t found = s1.find(s2);
  41. if (found != string::npos)
  42. return i+found;
  43. }
  44. return -1;
  45. }
  46. int main() {
  47. string s1,s2;
  48. char c;
  49. int i;
  50. s1 = "hack on hacker hack";
  51. s2 = "hack";
  52. c = 'Y';
  53. i = 7;
  54. int ans = solve(s1, s2, c, i);
  55. if(ans == -1)
  56. cout<<"Goodbye Watson"<<endl;
  57. else
  58. cout<<ans<<endl;
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5448KB
stdin
we love to hack on hackerearth
hack
Y
0
stdout
15