fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve(vector<string>& ans, vector<string> vs, int n, int k, string t) {
  5. if (k == n) {
  6. ans.push_back(t);
  7. return;
  8. }
  9. for (auto it: vs[k]) {
  10. t += it;
  11. solve(ans, vs, n, k+1, t);
  12. t.pop_back();
  13. }
  14. }
  15.  
  16. int main() {
  17. // your code goes here
  18. string s = "{a,b,c}d{e,f}g{h,i,j}";
  19. bool flag = false;
  20. vector<string> vs;
  21. int k = 0;
  22. for (int i=0;i<s.length();i++) {
  23. if (s[i] == '{') {
  24. flag = true;
  25. vs.push_back("");
  26. continue;
  27. }
  28. if (s[i] == '}' && flag) {
  29. flag = false;
  30. k++;
  31. continue;
  32. }
  33. if (s[i] == ',') { continue; }
  34. if (flag) {
  35. vs[k] += s[i];
  36. } else {
  37. string tmp = "";
  38. tmp += s[i];
  39. vs.push_back(tmp);
  40. k++;
  41. }
  42. }
  43.  
  44. for (auto it: vs) {
  45. cout<<it<<endl;
  46. }
  47. cout<<endl<<endl;
  48.  
  49. vector<string> ans;
  50. solve(ans, vs, k, 0, "");
  51. for (auto it: ans) {
  52. cout<<it<<endl;
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
abc
d
ef
g
hij


adegh
adegi
adegj
adfgh
adfgi
adfgj
bdegh
bdegi
bdegj
bdfgh
bdfgi
bdfgj
cdegh
cdegi
cdegj
cdfgh
cdfgi
cdfgj