C++ 最近的字符串:查找字符串数组中两个字符串之间的最小距离
您将得到一个字符串数组,后跟两个单词。您必须找到给定字符串数组中两个单词之间的最小距离 例如: (“快速”,“棕色”,“狐狸”,“快速”) 距离(“狐狸”,“那个”)=3 距离(“快速”、“狐狸”)=1 为什么该代码在下面给出的测试用例中失败C++ 最近的字符串:查找字符串数组中两个字符串之间的最小距离,c++,algorithm,C++,Algorithm,您将得到一个字符串数组,后跟两个单词。您必须找到给定字符串数组中两个单词之间的最小距离 例如: (“快速”,“棕色”,“狐狸”,“快速”) 距离(“狐狸”,“那个”)=3 距离(“快速”、“狐狸”)=1 为什么该代码在下面给出的测试用例中失败 #include<iostream> #include<string> #include<bits/stdc++.h> using namespace std; int search(vector<string
#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;
}
#包括
#包括
#包括
使用名称空间std;
整数搜索(矢量,字符串s1)
{
对于(int i=0;i>t;
而(t--)
{
int n;
cin>>n;
向量v;
对于(int i=0;i>a;
v、 推回(a);
}
字符串s1;
cin>>s1;
字符串s2;
cin>>s2;
int p,y;
p=搜索(v,s1);
y=搜索(v,s2);
int d=绝对值(p-y);
cout以下修改代码中突出显示了三个问题:
int search(const vector<string>&v, const string& s1) // use pass by const reference for performance here
{
for (int i = 0; i < v.size(); i++)
{
if (v[i].compare(s1) == 0) // use std::string::compare to compare two strings
{
return i;
}
}
return -1;
}
int main{
/* other codes */
p = search(v, s1);
y = search(v, s2);
if (p != -1 && y != -1) { // display output only when both searches are successful
int d = abs(p - y);
cout << d << endl;
}
}
int-search(const-vector&v,const-string&s1)//在这里使用pass-by-const引用来获得性能
{
对于(int i=0;i cout您的代码没有处理输入数组中的重复字符串
例如,如果输入数组是[“ghi”、“abc”、“abc”、“abc”、“ghi”、“def”、“ghi”、“def”]
那么“abc”和“def”之间的最小距离应该是:2(在索引3
和5
之间),但是您的代码将输出:4
代码失败,因为您有一个逻辑错误假设
如果您查看给定的测试字符串,您会发现有很多非常长的字符串(带有许多字符)。这应该会混淆测试集中只有5个不同字符串的事实
如果我们将这些长字符串替换为非常短的字符串,例如S和索引计数,那么输入将如下所示:
"S1", "S1", "S2", "S2", "S2", "S2", "S2", "S2", "S2", "S2", "S2", "S2", "S3",
"S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3",
"S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3",
"S3", "S3", "S3", "S3", "S3", "S3", "S3", "S4", "S4", "S4", "S4", "S4", "S5"
逻辑上与原文完全相同,但会使阅读和理解更加困难
使用新的测试用例表示法,您将立即看到问题。此列表中有许多重复。并且,如果您将示例S1和S2作为搜索词,那么您将在代码中找到位置0处的第一个S1和位置2处的第一个S1。您将计算2的距离。不正确
因此,您可以假设始终搜索字符串的最后一次出现是很好的,但这显然也不起作用。我们会将最后一个S1与最后一个S2进行比较。这也是错误的。因此,我们需要将最后找到的searchWord1与第一个找到的searchWord2进行比较
但这也不起作用,serach字符串可能以错误的顺序给出
向量中的其他地方可能有更多的S1-S2序列,我们需要检查它们
所有这些都会导致一个非常复杂的逻辑,所以我采用蛮力方法,将所有东西与所有东西进行比较。因此,如果我找到一个S1,那么我将与所有S2进行比较。我将对所有S1进行比较
请参阅下面的函数,它显示了我所做的操作。请注意。这只是许多可能性之一。请检查:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstdint>
const std::vector<std::string> testStringVector{
"S1", "S1", "S2", "S2", "S2", "S2", "S2", "S2", "S2", "S2", "S2", "S2", "S3",
"S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3",
"S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3", "S3",
"S3", "S3", "S3", "S3", "S3", "S3", "S3", "S4", "S4", "S4", "S4", "S4", "S5"
};
const std::string searchWord1{ "S3" };
const std::string searchWord2{ "S1" };
size_t minDistance(const std::vector<std::string>& stringVector, const std::string& s1, const std::string& s2)
{
unsigned int result = UINT_MAX;
// Compare everything with everything else
for (size_t i = 0; i < stringVector.size(); ++i) {
// Find the string s1
if (stringVector[i] == s1) {
for (size_t k = 0; k < stringVector.size(); ++k) {
// Do not compare to itself
if ((i != k) && (stringVector[k] == s2)) {
unsigned int distance = std::abs(static_cast<int>(i) - static_cast<int>(k));
if (distance < result)
result = distance;
}
}
}
}
return result;
}
int main()
{
std::cout << "\nMin Distance for '" << searchWord1 << "' and '" << searchWord2 << " is: "
<< minDistance(testStringVector, searchWord1, searchWord2) << "\n";
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
const std::vector testStringVector{
“S1”、“S1”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S2”、“S3”,
“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”,
“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”,
“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S3”、“S4”、“S4”、“S4”、“S4”、“S4”、“S5”
};
const std::string searchWord1{“S3”};
const std::string searchWord2{“S1”};
尺寸距离(常数标准::向量和字符串向量,常数标准::字符串和s1,常数标准::字符串和s2)
{
无符号整数结果=UINT_MAX;
//把每件事都和其他事情比较一下
对于(size_t i=0;i std::cout我希望我能很好地理解你的问题,如果是这样,那么这可能就行了(不确定,因为我只是用简单的案例测试它)。我在代码中添加了注释,而不是在这里解释。希望这不是问题
#include <vector>
#include <string>
#include <limits>
class MinimalDistance
{
private:
const std::vector<std::string> words;
struct indexed_word
{
unsigned int index;
std::string word;
indexed_word() {}
indexed_word(unsigned int index, const std::string& word) : index(index), word(word) {}
bool operator<(const indexed_word& iw) const
{ return this->index < iw.index; }
bool operator>(const indexed_word& iw) const
{ return this->index > iw.index; }
bool operator==(const indexed_word& iw) const
{ return this->index == iw.index; }
bool operator<=(const indexed_word& iw) const
{ return this->index <= iw.index; }
bool operator>=(const indexed_word& iw) const
{ return this->index >= iw.index; }
int operator-(const indexed_word& iw) const
{ return static_cast<int>(this->index) - static_cast<int>(iw.index); }
bool operator==(const std::string& s) const
{ return this->word == s; }
int operator=(const indexed_word& iw) const
{ return static_cast<int>(iw.index); }
};
// finds all occurrence of a word in the list
// given on construction
std::vector<indexed_word> find_all(const std::string& w)
{
// our indexed word list
std::vector<indexed_word> iws;
unsigned int i = 0;
// iterate through every word in the class' vector
for (const auto& word : words)
{
indexed_word iw(i, word);
// if we find the word in the list
// we put it in our indexed word list
if (word == w)
{
iws.push_back(iw);
}
// increment the index
i++;
}
return iws;
}
// helper function to find the minimal distance between an indexed word
// and a list of indexed words
// the indexed word vector contains the same words with different indexes
int min_dist_in(const indexed_word& iw, std::vector<indexed_word>& iws)
{
int min = npos;
// iterate through the given indexed word list
for (const auto& iword : iws)
{
// if the distance is smaller
// than the current min, change it
if (std::abs(iw - iword) < min)
{
min = std::abs(iw - iword);
}
}
return min;
}
public:
constexpr static int npos = std::numeric_limits<int>::max();
MinimalDistance(const std::vector<std::string>& words) : words(words) {}
int distance(const std::string& w1, const std::string& w2)
{
int min = npos;
std::vector<indexed_word> iws1 = find_all(w1);
std::vector<indexed_word> iws2 = find_all(w2);
// if either of our vectors contain 0 elements,
// return the 'not found min value'
if (!(iws1.size() || iws2.size()))
return min;
// iterate through one of the word lists
// the other will be the reference
for (const auto& iw : iws1)
{
// finds a minimal distance between indexed words
// in a given list
const int n = this->min_dist_in(iw, iws2);
// if this is smaller than the current minimum
// change assign this value to it instead;
if (n < min)
min = n;
}
return min;
}
};
int main()
{
std::vector<std::string> words{ "the", "quick", "brown", "fox", "is", "coming", "quick", "to", "catch", "another", "quick", "fox" };
MinimalDistance mindist(words);
int dist = mindist.distance("quick", "fox");
return 0;
}
#包括
#包括
#包括
类最小距离
{
私人:
常量std::向量词;
结构索引字
{
无符号整数索引;
字符串字;
索引单词(){}
索引单词(无符号整数索引,常量std::string&word):索引(index),单词(word){
布尔运算符索引(常量索引的单词和iw)常量
{返回此->索引>iw.index;}
布尔运算符==(常量索引的单词和iw)常量
{返回此->索引==iw.index;}
布尔运算符索引=(常量索引的单词和iw)常量
{返回此->索引>=iw.index;}
int运算符-(常量索引的单词和iw)常量
{返回static_cast(this->index)-static_cast(iw.index);}
布尔运算符==(常量std::string&s)常量
{返回此->单词==s;}
整型运算符=(常数索引的单词和iw)常数
{返回静态_cast(iw.index);}
};
//查找所有出现的wo
#include <vector>
#include <string>
#include <limits>
class MinimalDistance
{
private:
const std::vector<std::string> words;
struct indexed_word
{
unsigned int index;
std::string word;
indexed_word() {}
indexed_word(unsigned int index, const std::string& word) : index(index), word(word) {}
bool operator<(const indexed_word& iw) const
{ return this->index < iw.index; }
bool operator>(const indexed_word& iw) const
{ return this->index > iw.index; }
bool operator==(const indexed_word& iw) const
{ return this->index == iw.index; }
bool operator<=(const indexed_word& iw) const
{ return this->index <= iw.index; }
bool operator>=(const indexed_word& iw) const
{ return this->index >= iw.index; }
int operator-(const indexed_word& iw) const
{ return static_cast<int>(this->index) - static_cast<int>(iw.index); }
bool operator==(const std::string& s) const
{ return this->word == s; }
int operator=(const indexed_word& iw) const
{ return static_cast<int>(iw.index); }
};
// finds all occurrence of a word in the list
// given on construction
std::vector<indexed_word> find_all(const std::string& w)
{
// our indexed word list
std::vector<indexed_word> iws;
unsigned int i = 0;
// iterate through every word in the class' vector
for (const auto& word : words)
{
indexed_word iw(i, word);
// if we find the word in the list
// we put it in our indexed word list
if (word == w)
{
iws.push_back(iw);
}
// increment the index
i++;
}
return iws;
}
// helper function to find the minimal distance between an indexed word
// and a list of indexed words
// the indexed word vector contains the same words with different indexes
int min_dist_in(const indexed_word& iw, std::vector<indexed_word>& iws)
{
int min = npos;
// iterate through the given indexed word list
for (const auto& iword : iws)
{
// if the distance is smaller
// than the current min, change it
if (std::abs(iw - iword) < min)
{
min = std::abs(iw - iword);
}
}
return min;
}
public:
constexpr static int npos = std::numeric_limits<int>::max();
MinimalDistance(const std::vector<std::string>& words) : words(words) {}
int distance(const std::string& w1, const std::string& w2)
{
int min = npos;
std::vector<indexed_word> iws1 = find_all(w1);
std::vector<indexed_word> iws2 = find_all(w2);
// if either of our vectors contain 0 elements,
// return the 'not found min value'
if (!(iws1.size() || iws2.size()))
return min;
// iterate through one of the word lists
// the other will be the reference
for (const auto& iw : iws1)
{
// finds a minimal distance between indexed words
// in a given list
const int n = this->min_dist_in(iw, iws2);
// if this is smaller than the current minimum
// change assign this value to it instead;
if (n < min)
min = n;
}
return min;
}
};
int main()
{
std::vector<std::string> words{ "the", "quick", "brown", "fox", "is", "coming", "quick", "to", "catch", "another", "quick", "fox" };
MinimalDistance mindist(words);
int dist = mindist.distance("quick", "fox");
return 0;
}