一亿条数据去掉重复数据 谁有高效算法啊一亿条数据去掉重复数据 谁有高效算法啊

南瓜饼 2012-10-18 09:42:50
一亿条数据去掉重复数据 谁有高效算法啊
...全文
645 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fos_jpg 2012-11-06
  • 打赏
  • 举报
回复
引用 10 楼 xiang2yuner 的回复:
hash分解成小文件,然后对小文件进行处理,合并结果就可以了
请指教一下,hash如何分解?这么大的 读到内存里都够呛,分解也慢啊
xiang2yuner 2012-10-21
  • 打赏
  • 举报
回复
hash分解成小文件,然后对小文件进行处理,合并结果就可以了
npuhuxl 2012-10-19
  • 打赏
  • 举报
回复
如果数据都是int类型,可以用bitmap的方式
赵4老师 2012-10-19
  • 打赏
  • 举报
回复
仅供参考
//文件1中的内容排序并去重,结果保存到文件2中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 128 //能处理的最大行宽,包括行尾的\n和字符串尾的\0
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
int c,n,hh,i,L;
FILE *f;
char ln[MAXCHARS];
int ignore_case=0;
int icompare(const void *arg1,const void *arg2) {
return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
if (argc<3) {
printf("Unique line. Designed by zhao4zhong1@163.com. 2012-08-20\n");
printf("Usage: %s src.txt uniqued.txt [-i]\n",argv[0]);
return 1;
}
if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
f=fopen(argv[1],"r");
if (NULL==f) {
printf("Can not find file %s!\n",argv[1]);
return 1;
}
buf=(char *)malloc(MAXLINES*MAXCHARS);
if (NULL==buf) {
fclose(f);
printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
return 2;
}
n=0;
hh=0;
i=0;
while (1) {
if (NULL==fgets(ln,MAXCHARS,f)) break;//
hh++;
L=strlen(ln)-1;
if ('\n'!=ln[L]) {//超长行忽略后面内容
printf("%s Line %d too long(>%d),spilth ignored.\n",argv[1],hh,MAXCHARS);
while (1) {
c=fgetc(f);
if ('\n'==c || EOF==c) break;//
}
}
while (1) {//去掉行尾的'\n'和空格
if ('\n'==ln[L] || ' '==ln[L]) {
ln[L]=0;
L--;
if (L<0) break;//
} else break;//
}
if (L>=0) {
strcpy(buf+i,ln);i+=MAXCHARS;
n++;
if (n>=MAXLINES) {
MAXLINES2=MAXLINES*2;
if (MAXLINES2==1280000) MAXLINES2=2500000;
buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
if (NULL==buf2) {
printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
printf("WARNING: Lines >%d ignored.\n",MAXLINES);
break;//
}
buf=buf2;
MAXLINES=MAXLINES2;
}
}
}
fclose(f);
if (n>1) {
if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
else qsort(buf,n,MAXCHARS,compare);
}
f=fopen(argv[2],"w");
if (NULL==f) {
free(buf);
printf("Can not create file %s!\n",argv[2]);
return 2;
}
fprintf(f,"%s\n",buf);
if (n>1) {
if (ignore_case) {
hh=0;
L=MAXCHARS;
for (i=1;i<n;i++) {
if (stricmp((const char *)buf+hh,(const char *)buf+L)) {
fprintf(f,"%s\n",buf+L);
}
hh=L;
L+=MAXCHARS;
}
} else {
hh=0;
L=MAXCHARS;
for (i=1;i<n;i++) {
if ( strcmp((const char *)buf+hh,(const char *)buf+L)) {
fprintf(f,"%s\n",buf+L);
}
hh=L;
L+=MAXCHARS;
}
}
}
fclose(f);
free(buf);
return 0;
}
npuhuxl 2012-10-18
  • 打赏
  • 举报
回复
也可以把这些小文件分布到不同的机器上去算,这样就更好了
npuhuxl 2012-10-18
  • 打赏
  • 举报
回复
首先把数据hash到N多文件中,相同的数据肯定会在同一个文件中,这样问题的规模就会小很多;对每个小文件做处理,直接读到内存中,去掉重复就很容易了。
面试的时候,这种大规模的问题,hash一下百试百灵。
pathuang68 2012-10-18
  • 打赏
  • 举报
回复
在实际中,如果碰到这样的问题,就直接将数据弄到set里面就行了。
healer_kx 2012-10-18
  • 打赏
  • 举报
回复
baidu就那么几个题。
南瓜饼 2012-10-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

别说1亿条,说占了多少G内存吧
[/Quote]我也觉得蛋疼 这是百度出的面试题 恶心
taodm 2012-10-18
  • 打赏
  • 举报
回复
别说1亿条,说占了多少G内存吧

64,664

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧