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

古狗电面跪经

🔗
magicsets 2017-8-7 11:30:31 | 只看该作者
全局:
第三题将文本“分开”的方法,比较系统的知识应该是在"Programming Languages and Compilers"这类课程里前几章关于"Lexer/Parser"的部分。

只是要做题目的话,可以用一个stack去hack出来(stack=递归,是处理树状结构的很好的方法), 例如楼上提到的类似题目LeetCode 591,Discuss里给出的答案:
https://discuss.leetcode.com/topic/91300/java-solution-use-startswith-and-indexof

更通用一点的方法是写一个简单的“递归下降语法解析器”(recursive descent parser)——这个并不难写,而且可以解决几乎所有类似的题目,建议搜索并学习一下..

实践中需要解析文本的时候一般是用"parser generator"工具,比如flex+bison / JavaCC / Antlr之类的。这道题目非常适合词法和语法混合的LL(k)的parser generator,例如Antlr,语法部分几行代码就可以表达出来了。
回复

使用道具 举报

🔗
stargazer 2017-8-14 13:32:19 | 只看该作者
全局:
这么早就招全职了?还能LinkeIn直接联系HR?
回复

使用道具 举报

🔗
watercup 2017-8-15 02:28:45 | 只看该作者
全局:
第三题感觉就是设计一个DOM tree解析html 文件。
class TreeNode {
   String tag;
   String text;
   List<TreeNode> children;
}
回复

使用道具 举报

🔗
codemonk 2017-8-24 13:32:26 | 只看该作者
全局:
第三题 recursive descend parser

  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;

  5. struct TreeNode {
  6.     string tag;
  7.     string text;
  8.     vector<TreeNode*> children;
  9.     TreeNode(string tag_, string text_) {
  10.         tag = tag_;
  11.         text = text_;
  12.     }
  13. };

  14. TreeNode* parse(string& html, int& i) {
  15.     if(html[i] == '[') {
  16.         i++; // skip '['
  17.         int j = html.find(']', i);
  18.         string tag = html.substr(i, j-i);
  19.         i = j + 1;
  20.         
  21.         int start = i;
  22.         TreeNode* root = new TreeNode(tag, "");
  23.         while(html.substr(i,2) != "[/") {
  24.             auto child = parse(html, i);
  25.             if(child) root->children.push_back(child);
  26.         }
  27.         root->text = html.substr(start, i-start);
  28.         i += 2;
  29.         i = html.find(']', i);
  30.         i++;// skip ']'
  31.         return root;
  32.     }
  33.     else {
  34.         while(i < html.length() && html[i] != '[') i++; // scan all text content
  35.         return nullptr;
  36.     }
  37. }

  38. TreeNode* parseHtml(string& html) {
  39.     TreeNode* root = new TreeNode("","");
  40.     for(int i = 0; i < html.length(); ) {
  41.         auto tmp = parse(html, i);
  42.         if(tmp) root->children.push_back(tmp);
  43.     }
  44.     return root;
  45. }

  46. void printTree(TreeNode* root) {
  47.     queue<TreeNode*> q;
  48.     q.push(root);
  49.     while(!q.empty()) {
  50.         int qsize = q.size();
  51.         while(qsize-- > 0) {
  52.             auto top = q.front();
  53.             q.pop();
  54.             cout << "tag:" << top->tag << " text:" << top->text << "    ";
  55.             for(auto c : top->children) q.push(c);
  56.         }
  57.         cout << endl;
  58.     }
  59. }

  60. int main(int argc, const char * argv[]) {
  61.     // insert code here...
  62.     string html = "[text] Hello [/text] [bold]![/bold] [mark]word[/mark]";
  63.     printTree(parseHtml(html));
  64.     cout << endl;
  65.     html = "[text] Hello [/text] [bold]abc[tiny] maketiny [/tiny]123[/bold] [mark]word[/mark]";
  66.     printTree(parseHtml(html));
  67.     return 0;
  68. }
复制代码
回复

使用道具 举报

🔗
 楼主| FightForTomo 2017-8-24 13:36:50 | 只看该作者
全局:
codemonk 发表于 2017-8-24 13:32
第三题 recursive descend parser

谢谢,下次我就写出来了。
回复

使用道具 举报

🔗
jessyhann 2017-8-25 08:05:46 | 只看该作者
全局:
题主电面结果怎么样?
回复

使用道具 举报

🔗
littlegrass 2017-8-26 09:09:13 | 只看该作者
全局:
楼主有后续吗?
回复

使用道具 举报

🔗
 楼主| FightForTomo 2017-8-26 09:26:56 | 只看该作者
全局:

就继续刷题,争取再面一次呗。
回复

使用道具 举报

🔗
littlegrass 2017-8-26 09:39:08 | 只看该作者
全局:
FightForTomo 发表于 2017-8-26 09:26
就继续刷题,争取再面一次呗。

祝楼主好运!楼主的帖子写fresh grad 为什么不等今年的秋招 急着面SETI?
回复

使用道具 举报

🔗
 楼主| FightForTomo 2017-8-26 10:40:22 | 只看该作者
全局:
littlegrass 发表于 2017-8-26 09:39
祝楼主好运!楼主的帖子写fresh grad 为什么不等今年的秋招 急着面SETI?

年少无知唉
回复

使用道具 举报

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

本版积分规则

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