After trying a lot I’m still not able to figure out why its not being able to find if reverse of the word being searched for exists in the vector already or not.
Here is the problem link :
Here is the code
#include <bits/stdc++.h>
using namespace std;
string rev(string s) //Function to reverse string
{
string ans=" ";
for(int i=s.length();i>=0;i--)
ans = ans + s[i];
return ans;
}
char print(string x)// Function that Returns the character at the middle position of a given string
{
return x[(x.length()-1)/2];
}
int main()
{
int n;
cin>>n;
vector<string>v;
string str;
for(int i=0;i<n;i++)
{
cin>>str;
v.push_back(str);
}
for(int i=0;i<n;i++)
{
if(find(v.begin(),v.end(),rev(v[i]))!=v.end()) // String found yo
{
int len = v[i].length();
cout<<len<<" "<<print(v[i])<<endl;
break;
}
}
return 0;
}
Alternatively ideone link : http://ideone.com/e7T0FL
Thanks for reading