C++ 如何测试一个序列是否符合给定模式中的另一个序列?
下面描述的算法的名称是什么 说明: 如何测试一个序列是否符合给定模式中的另一个序列 例如:C++ 如何测试一个序列是否符合给定模式中的另一个序列?,c++,algorithm,C++,Algorithm,下面描述的算法的名称是什么 说明: 如何测试一个序列是否符合给定模式中的另一个序列 例如: The pattern: the number appears in the same order. bool isOrderValid(vector<int>& mainVec, vector<int>& subVec) { // how to implement this function? } test#1: isOrderValid({1, 2
The pattern: the number appears in the same order.
bool isOrderValid(vector<int>& mainVec, vector<int>& subVec) {
// how to implement this function?
}
test#1: isOrderValid({1, 2, 3, 4}, {2, 4}); // should return true
test#2: isOrderValid({1, 2, 3, 4}, {4, 2}); // should return false
isOrderValid({3, 6, 3, 1, 2, 3}, {3, 1, 3}); // should return true
这可以非常简单地实现(我在这里使用函数对象):
类基模式匹配器{
公众:
虚拟布尔运算符()(常数向量&a、常数向量&b)=0;
}
类的顺序模式匹配器:公共基模式匹配器{
公众:
布尔运算符()(常数向量&a、常数向量&b){
向量::迭代器iter_a=a.begin(),iter_b=b.begin();
while(iter_b!=b.end()&&iter_a!=a.end()){
if(*iter_b==*iter_a)
//我们在a中发现一个匹配b中搜索的元素的事件
//-->搜索下一个元素
iter_b++;
//检查下一个元素是否与搜索的元素匹配
iter_a++;
}
//我们已经在给定的顺序中找到了b的所有元素
返回iter_b==b.end();
};
}
isOrderValid(常数向量和a、常数向量和b、常数基\模式\匹配器和匹配器){
返回匹配器(a,b);
}
您可以为此编写标准的库样式算法。本例采用两个迭代器对并返回一个bool
:
#include <iostream>
#include <vector>
template <typename InIt1, typename InIt2>
bool is_subsequence(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2)
{
if (first2 == last2) {
return false; // sub empty (should this return true?)
}
for (; first1 != last1; ++first1) {
if (*first1 == *first2) {
if (++first2 == last2) {
return true; // sub exhausted
}
}
}
return false; // seq exhausted
}
int main()
{
std::vector<int> a = { 1, 2, 3, 4 }, b = { 2, 4 }, c = { 4, 2 };
std::vector<int> d = { 3, 6, 3, 1, 2, 3 }, e = { 3, 1, 3 };
std::cout << is_subsequence(a.begin(), a.end(), b.begin(), b.end()) << '\n';
std::cout << is_subsequence(a.begin(), a.end(), c.begin(), c.end()) << '\n';
std::cout << is_subsequence(d.begin(), d.end(), e.begin(), e.end()) << '\n';
}
#包括
#包括
模板
布尔是_子序列(InIt1 first1、InIt1 last1、InIt2 first2、InIt2 last2)
{
if(first2==last2){
返回false;//子空(是否返回true?)
}
对于(;first1!=last1;++first1){
如果(*first1==*first2){
如果(++first2==last2){
返回true;//子对象已耗尽
}
}
}
返回false;//seq已耗尽
}
int main()
{
向量a={1,2,3,4},b={2,4},c={4,2};
向量d={3,6,3,1,2,3},e={3,1,3};
std::cout这里有一个关于程序员堆栈交换的讨论,其中包括使用有限状态机:@PeterdeRivaz您的链接解决方案很棒!我喜欢迭代版本。
#include <iostream>
#include <vector>
template <typename InIt1, typename InIt2>
bool is_subsequence(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2)
{
if (first2 == last2) {
return false; // sub empty (should this return true?)
}
for (; first1 != last1; ++first1) {
if (*first1 == *first2) {
if (++first2 == last2) {
return true; // sub exhausted
}
}
}
return false; // seq exhausted
}
int main()
{
std::vector<int> a = { 1, 2, 3, 4 }, b = { 2, 4 }, c = { 4, 2 };
std::vector<int> d = { 3, 6, 3, 1, 2, 3 }, e = { 3, 1, 3 };
std::cout << is_subsequence(a.begin(), a.end(), b.begin(), b.end()) << '\n';
std::cout << is_subsequence(a.begin(), a.end(), c.begin(), c.end()) << '\n';
std::cout << is_subsequence(d.begin(), d.end(), e.begin(), e.end()) << '\n';
}
#include <iostream>
#include <list>
#include <vector>
template <typename InIt1, typename InIt2, typename Compare = std::equal_to<>>
bool is_subsequence(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, Compare cmp = Compare{})
{
if (first2 == last2) {
return false; // sub empty (should this return true?)
}
for (; first1 != last1; ++first1) {
if (cmp(*first1, *first2)) {
if (++first2 == last2) {
return true; // sub exhausted
}
}
}
return false; // seq exhausted
}
template <typename Seq, typename Sub, typename Compare = std::equal_to<>>
bool is_subsequence(const Seq &seq, const Sub &sub, Compare cmp = Compare{})
{
return is_subsequence(std::begin(seq), std::end(seq), std::begin(sub), std::end(sub), cmp);
}
int main()
{
std::vector<int> a = { 1, 2, 3, 4 }, b = { 2, 4 };
std::list<int> c = { 4, 2 };
std::vector<int> d = { 3, 6, 3, 1, 2, 3 };
int e[] = { 3, 1, 3 };
std::cout << is_subsequence(a, b) << '\n';
std::cout << is_subsequence(a, b, [](int lhs, int rhs) { return lhs == rhs; }) << '\n';
std::cout << is_subsequence(a, c) << '\n';
std::cout << is_subsequence(d, e) << '\n';
}