基本算法,求交集
来源:互联网 发布:js 中的map 编辑:程序博客网 时间:2024/05/21 22:14
一,交集运算算法
求多列数据的交集是一种应用广泛应用的算法,例如在搜索应用中,一个搜索关键词被分成了多个Term,每个Term对应一个倒排链表,那个整个搜索关键词的
搜索结果就是各个Term对应倒排链表元素的交集。
二,算法
/************************************************************************* > File Name: intersection.cpp > Author:zhangtx > Mail: 18510665908@163.com > Created Time: 2015年06月04日 星期四 17时19分17秒 ************************************************************************/#include <vector>#include<iostream>using namespace std;template<class T>void printResult(int left,int right,T data[]){ cout<<endl<<"**************************************************************"<<endl; for(int idx=left;idx<=right;idx++) cout<<data[idx]<<" ";}template<class T>int partition(int left,int right,T data[]){ T tmpData=data[(left+right)/2]; data[(left+right)/2]=data[left]; data[left]=tmpData; while(left<right) { while(data[right]>=tmpData && left<right) right--; data[left]=data[right]; while(data[left]<=tmpData && left<right) left++; data[right]=data[left]; } data[left]=tmpData; return left;}template<class T>void quickSort(int left,int right,T data[]){ if (left < right) { int pos=partition<int>(left,right,data); quickSort(left,pos-1,data); quickSort(pos+1,right,data); }}template<class T>void intersection(int testData[][12],int column){ int *index=new int[column]; int *count=new int[column]; for (int idx=0;idx<column;idx++) { count[idx]=sizeof(testData[idx])/sizeof(int); index[idx]=0; } int idx=0; while(true) { T max=testData[0][index[0]]; /*get current max value of each array * */ for(idx=0;idx<column;idx++) { if(testData[idx][index[idx]]>max) max=testData[idx][index[idx]]; } /* * move pos according to max * */ for(idx=0;idx<column;idx++) while(testData[idx][index[idx]]<max && index[idx]<count[idx]) index[idx]++; /* * wheather the current value of each array is equal * */ for(idx=0;idx<column-1;idx++) { if (testData[idx][index[idx]]!=testData[idx+1][index[idx+1]]) break; } if (idx==column-1) cout<<max<<" "; bool flag=false; for(idx=0;idx<column;idx++) { if (index[idx]>count[idx]) { /* * one array is over * */ flag=true; break; } /*step to next*/ index[idx]++; } if (flag==true) break; }}int main(int argc,char *argv[]){ int testDataA[][12]={ {53,5,7,19,12,324,46,76,928,111,2322,343}, {53,51,7,19,12,3224,46,76,928,111,232,22}, {53,51,7,19,12,3224,46,76,928,111,23222,11} }; //cout<<endl<<"sorting before"<<endl; int countA=sizeof(testDataA[0])/sizeof(int); //printResult<int>(0,countA-1,testDataA[0]); quickSort<int>(0,countA-1,testDataA[0]); cout<<endl<<"sorting result"<<endl; printResult<int>(0,countA-1,testDataA[0]); //cout<<endl<<"sorting before"<<endl; int countB=sizeof(testDataA[1])/sizeof(int); //printResult<int>(0,countB-1,testDataA[1]); quickSort<int>(0,countB-1,testDataA[1]); cout<<endl<<"sorting result"<<endl; printResult<int>(0,countB-1,testDataA[1]); //cout<<endl<<"sorting before"<<endl; int countC=sizeof(testDataA[2])/sizeof(int); //printResult<int>(0,countC-1,testDataA[2]); quickSort<int>(0,countC-1,testDataA[2]); cout<<endl<<"sorting result"<<endl; printResult<int>(0,countC-1,testDataA[2]); cout<<endl<<"intersection"<<endl;/* intersection algo*/ intersection<int>(testDataA,3); cout<<endl; return 0;}
三,运算
[root@M-192-168-10-225 algo]# g++ -g intersection.cpp [root@M-192-168-10-225 algo]# ./a.outsorting result**************************************************************5 7 12 19 46 53 76 111 324 343 928 2322 sorting result**************************************************************7 12 19 22 46 51 53 76 111 232 928 3224 sorting result**************************************************************7 11 12 19 46 51 53 76 111 928 3224 23222 intersection7 12 19 46 53 76 111 928
0 0
- 基本算法,求交集
- 基本归并算法(求两个集合的交集)
- 求反交集算法
- 区间求交集算法
- java字符串求交集算法
- 算法&大数据--(1)求交集
- 求交集
- 求交集
- 以单链表存储的两个集合求交集的算法
- 最快速度求两个数组之交集算法
- 【算法题集锦之二】--多个集合求交集
- 求交集和并集的线性算法
- 最快速度求两个数组之交集算法
- 最快速度求两个数组之交集算法与hash
- 求交集和并集的线性算法
- 算法 n1段区间 与 n2段区间求交集
- set求交集
- 用map求交集
- 深入Android SQLite 事务处理详解
- IntelliJ IDEA /Android Studio显示文档
- 关于android manager 不能更新的问题
- ubuntu找不到库编译问题的解决
- cocospod 之bug
- 基本算法,求交集
- jq data属性操作的一个陷阱
- python各种类型转换-int,str,char,float,ord,hex,oct等
- phpmailer
- CodeProject是个好地方
- Leetcode:Maximum Subarray
- 主键和索引的区别
- Hadoop YARN的发展史与详细解析
- Android笔记之:App应用之启动界面SplashActivity的使用