两个有序整数集合的交集怎么求
给定两个分别含m和n个有序整数的集合S1和S2,求交集有比O(m+n)更低复杂度的算法吗
------解决方案--------------------
有的,用量子计算机来算,复杂度为O(1)。
------解决方案--------------------
如果用Bits表示
求并的操作为 O(1)
转换为Bits表示(或者输入直接转换Bits表示)操作为O(M+N)
输出操作为 O(M+N)
------解决方案--------------------
求并的操作为 O(N/8)
------解决方案--------------------
求并的操作为 O((N+M)/8)=O(M+N)
不过操作不一样
不用比较操作,只需要Or 操作
------解决方案--------------------
仅供参考#include
#include
#include
#include
using namespace std;
int main() {
char *Alphabet = "abcdefghijklmnopqrstuvwxyz" ;
char *Vowels = "aeiou" ;
char *AlphaNum = "0123456789abcdef" ;
char result[45] ;
char *last ;
int lenA = strlen(Alphabet) ;
int lenV = strlen(Vowels ) ;
int lenAN = strlen(AlphaNum) ;
cout <
cout <
cout <
cout <
//non-predicate set_difference
last = set_difference(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result) ;
*last = 0 ;
cout <
//non-predicate set_intersection
last = set_intersection(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result) ;
*last = 0 ;
cout <
//non-predicate set_symmetric_difference
last = set_symmetric_difference(Alphabet, Alphabet+lenA,
Vowels , Vowels +lenV,
result) ;
*last = 0 ;
cout <
//non-predicate set_union
last = set_union(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result) ;
*last = 0 ;
cout <
cout <
//predicate set_difference
last = set_difference(Alphabet, Alphabet+lenA,
AlphaNum, AlphaNum+lenAN,
result , less()) ;
*last = 0 ;
cout <