农民领袖
- 积分
- 11892
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-7-23
- 最后登录
- 1970-1-1
|
刚才咋没贴出来……
- vector<string> getNested(string s) {
- s += ',';
- vector<string> res, last{""}, cur, tmp;
-
- for (int i=0; i<s.size(); i++) {
- if (s[i] == ',') {
- for (int j=0; j<last.size(); j++)
- res.push_back(last[j]);
-
- last = {""};
- } else {
- cur.clear();
- if (s[i] == '{') {
- int count = 1;
- int j = i+1;
- while (j<s.size() && count>0) {
- if (s[j]=='{') count++;
- else if (s[j]=='}') count--;
- j++;
- };
- // cout << i << " " << j << endl;
- // cout << s.substr(i+1, j-i-2) << endl;
- cur = getNested(s.substr(i+1, j-i-2));
- i = j-1;
- } else
- cur.push_back(string(1, s[i]));
-
- tmp.clear();
- for (int j=0; j<last.size(); j++) {
- for (int k=0; k<cur.size(); k++) {
- tmp.push_back(last[j] + cur[k]);
- // cout << tmp.back() << " ";
- }
- // cout << endl;
- }
- last = tmp;
- }
- }
- return res;
- }
- vector<string> expandS(string s) {
- vector<string> res = getNested(s);
- for (auto r : res)
- cout << r << " ";
- cout << endl;
- return res;
- }
- int main() {
- expandS("a,b,c");
- expandS("a{b,c}");
- expandS("{a,b}{c,d}");
- expandS("{a,b}{c,d},e");
- expandS("f{a,b}{c,d},e");
- expandS("a{c{a,b}}");
- expandS("d{c,{a,b}}");
- expandS("a{b,c}d");
- return 0;
- }
复制代码 |
|