fork download
  1. /*
  2. Source Code by adityaghosh996
  3. */
  4. #include<bits/stdc++.h>
  5. #define ll long long int
  6. #define pb(a) push_back(a)
  7. #define mp(a,b) make_pair(a,b)
  8. using namespace std;
  9. string name[1000000];
  10. vector<ll> child;
  11. vector<string> level[1000000];
  12. bool vis[1000000];
  13. ll n,pos=0,minuend=0;
  14. inline bool isnum(string s)
  15. {
  16. for(ll i=0;i<s.length();i++)
  17. {
  18. if(!isdigit(s[i]))
  19. return false;
  20. }
  21. return true;
  22. }
  23. inline ll numval(string s)
  24. {
  25. ll val=0;
  26. for(ll i=0;i<s.length();i++)
  27. {
  28. int d=s[i]-'0';
  29. val=val*10+d;
  30. }
  31. return val;
  32. }
  33. inline void dfs(ll curr_level)
  34. {
  35. level[curr_level-minuend].pb(name[pos]);
  36. vis[pos]=true;
  37. ll ch=child[pos];
  38. pos++;
  39. for(ll i=0;i<ch;i++)
  40. {
  41. dfs(curr_level+1);
  42. }
  43. return;
  44. }
  45. int main()
  46. {
  47. ios_base::sync_with_stdio(0);;cin.tie(0);
  48. memset(vis,false,sizeof(vis));
  49. string s;
  50. cin>>s;
  51. vector<string> total;
  52. string temp="";
  53. for(ll i=0;i<s.length();i++)
  54. {
  55. if(s[i]==',')
  56. {
  57. total.pb(temp);
  58. temp="";
  59. }
  60. else
  61. temp=temp+s[i];
  62. }
  63. total.pb(temp);
  64. ll cnt=0;
  65. for(ll i=0;i<total.size();i++)
  66. {
  67. if(isnum(total[i]))
  68. {
  69. child.pb(numval(total[i]));
  70. cnt++;
  71. }
  72. else
  73. {
  74. name[cnt]=total[i];
  75. }
  76. }
  77. n=child.size();
  78. while(pos<n)
  79. {
  80. minuend=pos;
  81. dfs(pos);
  82. }
  83. ll tlev=0;
  84. for(ll i=0;i<1000000;i++)
  85. {
  86. tlev++;
  87. if(level[i].size()==0)
  88. break;
  89. }
  90. cout<<tlev-1<<endl;
  91. for(ll i=0;i<1000000;i++)
  92. {
  93. if(level[i].size()==0)
  94. break;
  95. for(ll j=0;j<level[i].size();j++)
  96. cout<<level[i][j]<<" ";
  97. cout<<endl;
  98. }
  99. return 0;
  100. }
Success #stdin #stdout 0s 20080KB
stdin
Standard input is empty
stdout
1