Ý bạn có phải làm xét xem tập hợp A có phải là con của tập hợp B (kiểm tra xem tất cả các phần tử của A có tồn tại hết trong B hay không) ? Nếu là kiểm tra theo kiểu tập hợp thì bạn thử theo cách của mình:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool check(vector<int> a, vector<int> b) {
int ia = 0, ib = 0; //index of vector a and index of vector b
while(b[ib] < a[ia] && ib < b.size())
ib++; //move the index of vector b to position
//where a[ia] = b[ib]
//first of all, we check if the next element of vector b
//whether equal with the first element of vector a
//if not, return false
if(b[ib] != a[ia])
return false;
//next, we check if each respective element whether equal or not
while(ia < a.size() && ib < b.size())
{
if(a[ia] != b[ib])
return false;
else
ia++, ib++;
}
return true;
}
int main() {
vector<int> a = { 4,2,3 };
vector<int> b = { 1,2,3,4,5,6 };
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
cout << check(a,b);
return 0;
}
Vì kiểm tra tập hợp, nên mình đảm bảo là đã loại bỏ các phần tử bị trùng trong mỗi vector, và vector con cần kiểm tra phải có số lượng phần tử nhỏ hơn vector kia.
Bạn sắp xếp mảng kiểu string tương tự như kiểu số nguyên.