题目描述
思路介绍:
①使用vector存储AB
②使用unique与erase对AB分别进行去重
③开辟一个标记数组,使用二重循环外层遍历A内层遍历B,如果在A也在B的在数组中标记,如果不在就不做标记
④输出不做标记的内容
附代码:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std ;
int main()
{
int number = 0 ;//插入去重函数
cin >> number ;
for( int k = 0 ; k < number ; k ++ )
{
vector<int> arr_first ;
vector<int> arr_second ;
int test_first = 0 ;
cin >> test_first ;
for( int j = 0 ; j < test_first ; j ++ )
{
int temp = 0 ;
cin >> temp ;
arr_first.push_back(temp) ;
}
int test_second = 0 ;
cin >> test_second ;
for(int j = 0 ; j < test_second ; j ++)
{
int temp = 0 ;
cin >> temp ;
arr_second.push_back(temp) ;
}
//第一个去重
sort(arr_first.begin(),arr_first.end());
auto temp_arr = unique(arr_first.begin(),arr_first.end()) ;
arr_first.erase(temp_arr,arr_first.end()) ;
//第二个去重
sort(arr_second.begin(),arr_second.end());
temp_arr = unique(arr_second.begin(),arr_second.end()) ;
arr_second.erase(temp_arr,arr_second.end()) ;
int size = arr_first.size() ;
int *arr_judge = new int[size]() ;
for( int i = 0 ; i < size ; i ++ )
{
arr_judge[i] = 1 ;
}
for(int i = 0 ; i < arr_first.size() ; i ++ )
{
for(int j = 0 ; j < arr_second.size() ; j++ )
{
if(arr_first[i] == arr_second[j])
{
arr_judge[i] = 0 ;
break ;
}
}
}
int total = 0 ;
for(int i = 0 ; i < size ; i ++ )
{
if( arr_judge[i] == 1 )
{
total ++ ;
//cout << arr_first[i] << ' ' ;
}
}
for(int i = 0 ; i < size ; i ++ )
{
if(arr_judge[i] == 1 && total > 1)
{
cout << arr_first[i] << ' ' ;
total -- ;
}
else if(arr_judge[i] == 1 && total == 1)
{
cout << arr_first[i] ;
}
}
cout << endl ;
delete []arr_judge ;
}
}
提交之后结果是:
看了一下代码,大概是判断在A不在B的元素时候使用的二重循环,导致时间复杂度为n^2,从而导致的超时,所以这里可以优化一下算法,内层使用二分查找,对内层在A不在B的元素进行查找,所以优化代码之后:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std ;
//二分查找
bool binSearch(vector<int> s, const int size, const int target)
{
int start = 0 ;
int end = size - 1 ;
bool judge = false;
int mid = ( end - start ) / 2 + start ;
while (start <= end)
{
mid = ( end - start ) / 2 + start ;
if (s[mid] < target)
{
start = mid + 1;
}
else if (s[mid] == target)
{
judge = true ;
return 1 ;
break ;
}
else
{
end = mid - 1 ;
}
}
if (judge == false)
{
return false ;
}
}
int main()
{
int number = 0 ;//插入去重函数
cin >> number ;
for( int k = 0 ; k < number ; k ++ )
{
vector<int> arr_first ;
vector<int> arr_second ;
int test_first = 0 ;
cin >> test_first ;
for( int j = 0 ; j < test_first ; j ++ )
{
int temp = 0 ;
cin >> temp ;
arr_first.push_back(temp) ;
}
int test_second = 0 ;
cin >> test_second ;
for(int j = 0 ; j < test_second ; j ++)
{
int temp = 0 ;
cin >> temp ;
arr_second.push_back(temp) ;
}
//第一个去重
sort(arr_first.begin(),arr_first.end());
auto temp_arr = unique(arr_first.begin(),arr_first.end()) ;
arr_first.erase(temp_arr,arr_first.end()) ;
//第二个去重
sort(arr_second.begin(),arr_second.end());
temp_arr = unique(arr_second.begin(),arr_second.end()) ;
arr_second.erase(temp_arr,arr_second.end()) ;
int size = arr_first.size() ;
int *arr_judge = new int[size]() ;
for( int i = 0 ; i < size ; i ++ )
{
arr_judge[i] = 1 ;
}
for(int i = 0 ; i < arr_first.size() ; i ++ )
{
bool temp = binSearch(arr_second,arr_second.size(),arr_first[i]) ;
if(temp == true)
{
arr_judge[i] = 0 ;
}
}
int total = 0 ;
for(int i = 0 ; i < size ; i ++ )
{
if( arr_judge[i] == 1 )
{
total ++ ;
//cout << arr_first[i] << ' ' ;
}
}
for(int i = 0 ; i < size ; i ++ )
{
if(arr_judge[i] == 1 && total > 1)
{
cout << arr_first[i] << ' ' ;
total -- ;
}
else if(arr_judge[i] == 1 && total == 1)
{
cout << arr_first[i] ;
}
}
cout << endl ;
delete []arr_judge ;
}
}
这次就可以过了(抱拳)