📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 1985| 回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

Google : 找相同的文件

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
有一堆文件, 把它们分组,就是说内容完全相同的文件归类到一组

上一篇:Google/Microsoft : Brain test
下一篇:Google: 找公共prefix
🔗
iveney 2011-8-24 09:52:50 | 只看该作者
全局:
My idea: practically, 先用文件size把不一致的分類; 然後計算 MD5 進行分類.
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-8-24 10:10:07 | 只看该作者
全局:
My idea: practically, 先用文件size把不一致的分類; 然後計算 MD5 進行分類.
iveney 发表于 2011-8-24 09:52



    不会MD5, 自己写了个简单的编码函数,不知道这种题几个人能在现场写出runable的code

  1. int EnCodeFile(const char* strFile)
  2. {
  3.         assert(strFile);

  4.         FILE* fp = fopen(strFile, "r");
  5.         if (NULL == fp) return 0;

  6.         int nCode = 0;
  7.         char c;
  8.         while ((c = fgetc(fp)) != EOF)
  9.         {
  10.                 char tmp = (nCode >> 24);
  11.                 nCode = (nCode << 8);
  12.                 nCode = nCode ^ c;
  13.                 nCode = nCode ^ tmp;
  14.         }

  15.         fclose(fp);
  16.         return nCode;
  17. }

  18. bool CheckContent(const char* strFile1, const char* strFile2)
  19. {
  20.         assert(strFile1 && strFile2);

  21.         FILE* fp1 = fopen(strFile1, "r");
  22.         if (NULL == fp1) return false;

  23.         FILE* fp2 = fopen(strFile2, "r");
  24.         if (NULL == fp2) return false;

  25.         char c1, c2;
  26.         bool bRet = true;
  27.         while (true)
  28.         {
  29.                 c1 = fgetc(fp1);
  30.                 c2 = fgetc(fp2);

  31.                 if (EOF == c1 || EOF == c2 || c1 != c2)
  32.                         break;
  33.         }

  34.         if (c1 != EOF || c2 != EOF)
  35.                 bRet = false;

  36.         fclose(fp1);
  37.         fclose(fp2);

  38.         return bRet;
  39. }

  40. void PrintSameEnCoding(vector<char*>& vec)
  41. {
  42.         while (!vec.empty())
  43.         {
  44.                 char* strFile = *(vec.begin());
  45.                 cout<<strFile<<" ";
  46.                 vec.erase(vec.begin());

  47.                 for (vector<char*>::iterator it = vec.begin();
  48.                         it != vec.end();)
  49.                 {
  50.                         if (CheckContent(strFile, *it))
  51.                         {
  52.                                 cout<<*it<" ";
  53.                                 it = vec.erase(it);
  54.                         }
  55.                         else
  56.                                 it++;
  57.                 }

  58.                 cout<<endl;
  59.         }
  60. }

  61. void FindSameFiles(char* files[], int n)
  62. {
  63.         assert(files && n > 0);

  64.         hash_map<int, vector<char*>> map;

  65.         for (int i = 0; i < n; i++)
  66.         {
  67.                 int nCode = EnCodeFile(files[i]);

  68.                 if (map.find(nCode) == map.end())
  69.                 {
  70.                         vector<char*> vec;
  71.                         vec.push_back(files[i]);
  72.                         map[nCode] = vec;
  73.                 }
  74.                 else
  75.                         map[nCode].push_back(files[i]);
  76.         }

  77.         for (hash_map<int, vector<char*>>::iterator it = map.begin();
  78.                 it != map.end(); it++)
  79.         {
  80.                 PrintSameEnCoding(it->second);
  81.         }
  82. }
复制代码
回复

使用道具 举报

🔗
iveney 2011-8-24 22:53:23 | 只看该作者
全局:
這題是編程題? 聽起來更像是系統知識的題目吧.
MD5 估計也沒幾個人能不看文檔手寫出來吧...
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-8-25 00:14:47 | 只看该作者
全局:
這題是編程題? 聽起來更像是系統知識的題目吧.
MD5 估計也沒幾個人能不看文檔手寫出來吧...
iveney 发表于 2011-8-24 22:53



    这是要写出runable code 的google面试题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表