You are given a array of string followed by two words. You have to find the minimum distance between the two words in the given array of string
For example: (“the”, “quick”, “brown”, “fox”, “quick”) distance(“fox”,”the”) = 3 distance(“quick”, “fox”) = 1
Why is this code failing at the given below test case??
#include<iostream>
#include<string>
#include<bits/stdc++.h>
using namespace std;
int search(vector<string>v,string s1)
{
for(int i=0;i<v.size();i++)
{
if(v[i]==s1)
{
return i;
}
}
return -1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin >> n;
vector<string> v;
for(int i = 0; i < n; ++i)
{
string a;
cin >> a;
v.push_back(a);
}
string s1;
cin >> s1;
string s2;
cin >> s2;
int p, y;
p = search(v, s1);
y = search(v, s2);
int d = abs(p-y);
cout<<d<<endl;
}
return 0;
}
Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed:
Input: 52 rbkiruxixlqpjkbcdctwvsogiurmicjafuiwrhhqsyiflkjqodomwfvhanvirgjydtyudgnyhweujpmxtdmsiickxyvrffri rbkiruxixlqpjkbcdctwvsogiurmicjafuiwrhhqsyiflkjqodomwfvhanvirgjydtyudgnyhweujpmxtdmsiickxyvrffri ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw sqtfupuwqwvqwqtvqtqpytysnojdln ueimikiambnhdivnfbfigtqrckknhuefborlyaoo rbkiruxixlqpjkbcdctwvsogiurmicjafuiwrhhqsyiflkjqodomwfvhanvirgjydtyudgnyhweujpmxtdmsiickxyvrffri
Its Correct output is: 11
And Your Code's output is: 12