gpt4 book ai didi

c++ - 最近的字符串 :Find minimum distance between 2 strings in a string array

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:06:10 25 4
gpt4 key购买 nike

给定一个字符串数组,后跟两个单词。你必须在给定的字符串数组中找到两个单词之间的最小距离

例如:(“the”、“quick”、“brown”、“fox”、“quick”)距离(“狐狸”,“那个”)= 3距离(“快”,“狐狸”)= 1

为什么这段代码在下面给定的测试用例中失败了??

#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;
}

错误的答案。 !!!错误的答案可能您的代码对于多个测试用例 (TC) 无法正常工作。您的代码失败的第一个测试用例:

输入:52rbkiruxixlqpjkbcdctwvsogiurmicjafuiwrhhqsyiflkjqodomwfvhanvirgjydtyudgnyhweujpmxtdmsiickxyvrffri rbkiruxixlqpjkbcdctwvsogiurmicjafuiwrhhqsyiflkjqodomwfvhanvirgjydtyudgnyhweujpmxtdmsiickxyvrffri ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ffyvehskceaqevtqqectpasluasmkvdpbelhlgtqkw ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimiki ambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtqrckknhuefborlyaoo ueimikiambnhdivnfbfigtq rckknhuefborlyaoo xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw xgnuvsvdbcwtwahjpogvthlnvmpkbsocbcwwwubyxauwccgrtpqpw sqtfupuwqwvqwqtvqtqpytysnojdlnueimikiambnhdivnfbfigtqrckknhuefborlyaoo rbkiruxixlqpjkbcdctwvsogiurmicjafuiwrhhqsyiflkjqodomwfvhanvirgjydtyudgnyhweujpmxtdmsiickxyvrffri

它的正确输出是:11

您的代码的输出是:12

最佳答案

您的代码未处理输入数组中的重复字符串。

例如,如果输入数组是 ["ghi", "abc", "abc", "abc", "ghi", "def", "ghi", "def"]那么“abc”和“def”之间的最小距离应该是:2(在索引 35 之间)但是你的代码将输出:4

关于c++ - 最近的字符串 :Find minimum distance between 2 strings in a string array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57319806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com