查看: 1547| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[树/链表/图] 生成全部可能的字符串

全局:
高频题
公司名称: google

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

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

x
原题:
input a string: a{b,c}{d,e} ,return a list of string [abd, abe, acd, ace]

followup是如果有嵌套怎么办:
a{b,c{d,e}}

在地里看到几次了。如果没有嵌套,直接用backtracking就可以。但是有嵌套的话该怎么办?都不知道followup该生成什么..

上一篇:求问有没有针对LeetCode题目进行讲解的视频?
下一篇:推荐和分享资料- Problem Solving with Algorithms and Data Structures using P...
🔗
14417335 2019-6-11 20:24:38 | 只看该作者
全局:
可以先理解{b,c{d,e}}能生成什么?根据观察到的规则,{b, cd, ce}

那么,a{b, cd, ce}能生成什么?ab, acd, ace
回复

使用道具 举报

全局:
比起理解输出是什么 我觉得parse这个嵌套的string比较麻烦
回复

使用道具 举报

🔗
pandami 2019-6-12 02:44:32 | 只看该作者
全局:
能写码就不bb 写了一个有点繁琐的

  1. class ExpandString {
  2. public:
  3.   vector<string> tokenize(string & str) {
  4.     cout << "TOK: " << str << endl;
  5.     vector<string> tokens;
  6.     str.push_back(',');
  7.     int start = 0;
  8.     for (int i = 0; i < str.size(); ++i) {
  9.       if (isalpha(str[i])) {
  10.         continue;
  11.       } else if (str[i] == '{') {
  12.         int cnt = 0;
  13.         while (true) {
  14.           if (str[i] == '{') {
  15.             ++cnt;
  16.           } else if (str[i] == '}') {
  17.             --cnt;
  18.           }
  19.           if (cnt == 0) {
  20.             break;
  21.           }
  22.           ++i;
  23.         }
  24.       } else if (str[i] == ',') {
  25.         string w = str.substr(start, i - start);
  26.         start = i + 1;
  27.         vector<string> res = expand(w);
  28.         copy(res.begin(), res.end(), back_inserter(tokens));
  29.       }
  30.     }
  31.     return tokens;
  32.   }
  33.   vector<string> expand(string & str) {
  34.     cout << "EXP: " << str << endl;
  35.     auto loc = str.find('{');
  36.     if (loc == string::npos) {
  37.       return {str};
  38.     }

  39.     vector<string> out = {""};

  40.     for (int i = 0; i < str.size(); ++i) {
  41.       if (isalpha(str[i])) {
  42.         int e = i;
  43.         while (e < str.size() && isalpha(str[e])) {
  44.           ++e;
  45.         }
  46.         string w = str.substr(i, e - i);
  47.         for (int j = 0; j < out.size(); ++j) {
  48.           out[j] = out[j] + w;
  49.         }
  50.         i = e - 1;
  51.       } else {
  52.         // bracket
  53.         int cnt = 0;
  54.         int e = i;
  55.         while (true) {
  56.           if (str[e] == '{') {
  57.             ++cnt;
  58.           } else if (str[e] == '}') {
  59.             --cnt;
  60.           }
  61.           if (cnt == 0) {
  62.             break;
  63.           }
  64.           ++e;
  65.         }
  66.         string w = str.substr(i + 1, e - i - 1);
  67.         vector<string> ws = tokenize(w);
  68.         vector<string> base;
  69.         base.swap(out);
  70.         for (auto & s1: base) {
  71.           for (auto & s2: ws) {
  72.             out.push_back(s1 + s2);
  73.           }
  74.         }
  75.         i = e;
  76.       }
  77.     }
  78.     return out;
  79.   }
  80. };
复制代码


Test Case

  1. int main() {
  2.   string input = "{x,y}a{b,c,e{d,f}}";
  3.   ExpandString es;
  4.   vector<string> result = es.expand(input);
  5.   for (auto s: result) {
  6.     cout << s << " ";
  7.   }
  8.   cout << endl;
  9. }
复制代码



OUTPUT:
  1. EXP: {x,y}a{b,c,e{d,f}}
  2. TOK: x,y
  3. EXP: x
  4. EXP: y
  5. TOK: b,c,e{d,f}
  6. EXP: b
  7. EXP: c
  8. EXP: e{d,f}
  9. TOK: d,f
  10. EXP: d
  11. EXP: f
  12. xab xac xaed xaef yab yac yaed yaef
复制代码


评分

参与人数 1大米 +1 收起 理由
薏米红豆芡实 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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