#include <iostream>
#include<string.h>
using namespace std;
int length_last(string str)
{
int count = 0;
bool flag = false;
for (int i = str.length() - 1; i >= 0; i--) {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
flag = true;
count++;
}
else {
if (flag == true)
return count;
}
}
return count;
}
int solve(string s1, string s2, char c, int i){
string temp = s1;
s1 = s1.substr(i, s1.length());
if(c == 'Y'){
size_t f1 = s1.find(s2 + " ");
if (f1 != string::npos && i+f1 == 0)
return i+f1;
size_t f2 = s1.find(" " + s2 + " ");
if (f2 != string::npos)
return i+f2+1;
size_t f3 = s1.find_last_of(" " + s2);
// cout<<i+f3;
// cout<<temp.length();
if (f3 != string::npos)
return (i+f3)-length_last(temp)+1;
}
else {
size_t found = s1.find(s2);
if (found != string::npos)
return i+found;
}
return -1;
}
int main() {
string s1,s2;
char c;
int i;
s1 = "hack on hacker hack";
s2 = "hack";
c = 'Y';
i = 7;
int ans = solve(s1, s2, c, i);
if(ans == -1)
cout<<"Goodbye Watson"<<endl;
else
cout<<ans<<endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZTxzdHJpbmcuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCgppbnQgbGVuZ3RoX2xhc3Qoc3RyaW5nIHN0cikKewogICAgaW50IGNvdW50ID0gMDsKICAgIGJvb2wgZmxhZyA9IGZhbHNlOwogICAgZm9yIChpbnQgaSA9IHN0ci5sZW5ndGgoKSAtIDE7IGkgPj0gMDsgaS0tKSB7CiAgICAgICAgaWYgKChzdHJbaV0gPj0gJ2EnICYmIHN0cltpXSA8PSAneicpIHx8IChzdHJbaV0gPj0gJ0EnICYmIHN0cltpXSA8PSAnWicpKSB7CiAgICAgICAgICAgIGZsYWcgPSB0cnVlOwogICAgICAgICAgICBjb3VudCsrOwogICAgICAgIH0KICAgICAgICBlbHNlIHsKICAgICAgICAgICAgaWYgKGZsYWcgPT0gdHJ1ZSkKICAgICAgICAgICA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