📣 独立日限时特惠: VIP通行证立减$68
楼主: catherineycycy
跳转到指定楼层
上一主题 下一主题
收起左侧

狗家Fall Intern电面

🔗
codemonk 2017-8-25 02:27:16 | 只看该作者
全局:
第一题 立扣 糁舅舅
回复

使用道具 举报

🔗
codemonk 2017-8-25 05:09:10 | 只看该作者
全局:
zhangsikai123 发表于 2017-7-13 10:26
第二题个人感觉应该属于人工智能领域的CSP问题,类似八皇后问题,本身是NP-hard,但也有很多优化方法,这里 ...

N 皇后这个思路很赞!  dp解超过我的能力了
回复

使用道具 举报

🔗
codemonk 2017-8-25 05:40:31 | 只看该作者
全局:
zhangsikai123 发表于 2017-7-13 10:26
第二题个人感觉应该属于人工智能领域的CSP问题,类似八皇后问题,本身是NP-hard,但也有很多优化方法,这里 ...

参照大神点拔的八皇后思路。用bit manipulation 存状态 做了backtracking. 如下。大神的dp解更优。我这个可能稍微适合普通玩家理解一点。
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;

  4. #define N 6  // N is the length of target string

  5. bool helper(vector<string>& strs, string target, int state, int pos){
  6.    
  7.     // calculate cross: indices of chars in target that also appear in strs[pos]. Those indices are set at the corresponding bit in "cross".
  8.     if(state == (1 << N) - 1) return true; // all bits are set
  9.     if(pos == N) return false; // already tried all strings in strs without success
  10.    
  11.     int cross = 0;
  12.     string& curr = strs[pos];
  13.     for(int i = 0; i < N; i++) {
  14.         if(count(curr.begin(), curr.end(), target[i]))
  15.             cross |= (1 << i);
  16.     }
  17.     int cand = cross & (~state);
  18.     for(; cand != 0; cand -= cand & (-cand)) {
  19.         if(helper(strs, target, state + (cand&(-cand)), pos+1)) return true;
  20.     }
  21.     return false;
  22. }

  23. bool makeTarget(vector<string>& strs, string target) {
  24.     int state = 0;
  25.     return helper(strs, target, state, 0);
  26. }

  27. int main(int argc, const char * argv[]) {
  28.     // insert code here...
  29.     vector<string> strs = {
  30.         "abcdef",
  31.         "abcdef",
  32.         "abcdef",
  33.         "abcdef",
  34.         "abcdef",
  35.         "abcdef"
  36.     };
  37.     cout << helper(strs, "abcdef", 0, 0) << endl;
  38.    
  39.     strs = vector<string> ({
  40.         "abcdef",
  41.         "abcdef",
  42.         "abcdef",
  43.         "abcdef",
  44.         "abcdef"
  45.     });
  46.     cout << helper(strs, "abcdef", 0, 0) << endl;
  47.    
  48.     strs = vector<string> ({
  49.         "axxxxx",
  50.         "abyyyy",
  51.         "abczzz",
  52.         "abcdqq",
  53.         "abcdep",
  54.         "lllllf"
  55.     });
  56.     cout << helper(strs, "abcdef", 0, 0) << endl;
  57.     return 0;
  58. }
复制代码
回复

使用道具 举报

🔗
zhangsikai123 2017-8-25 07:50:18 | 只看该作者
全局:
codemonk 发表于 2017-8-25 05:09
N 皇后这个思路很赞!  dp解超过我的能力了

正解应该是网络流,Edmonds-Karp算法: https://en.wikipedia.org/wiki/Maximum_flow_problem, CLRS上也有。这俩人靠这个算法拿了图灵奖....如果半小时空想想出来那只可能是神。
回复

使用道具 举报

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

本版积分规则

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