fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. std::vector<std::vector<std::pair<char, int>>> ToVector(std::string& str){
  7. std::vector<std::vector<std::pair<char, int>>> vec;
  8. std::size_t j = 0;
  9.  
  10. vec.push_back(std::vector < std::pair<char, int>>());
  11.  
  12. for (std::size_t i = 0; i < str.size(); i++){
  13. if (str[i] == '\n'){
  14. vec.push_back(std::vector < std::pair<char, int>>());
  15. j++;
  16. continue;
  17. }
  18. vec[j].push_back(std::make_pair(str[i], 0));
  19. }
  20. return vec;
  21. }
  22.  
  23. bool makehoge(std::string& str){
  24. auto vec = ToVector(str);
  25. std::vector<std::size_t> Count;
  26. Count.resize(vec.size());
  27.  
  28. bool F = true;
  29. char M = 127;
  30. bool F2 = false;
  31. while (1){
  32. F2 = false;
  33. M = 127;
  34. for (std::size_t i = 1; i < Count.size(); i++){
  35. if (Count[i-1] == vec[i-1].size())continue;
  36. if (Count[i] == vec[i].size())continue;
  37. F2 = true;
  38.  
  39. F &= (vec[i][Count[i]].first == vec[i - 1][Count[i - 1]].first);
  40. M = std::min(M, vec[i][Count[i]].first);
  41. M = std::min(M, vec[i - 1][Count[i - 1]].first);
  42.  
  43. }
  44. if (F2 == false)break;
  45. if (F == true){
  46. for (auto& o : Count)o++;
  47. }
  48. else{
  49.  
  50. for (std::size_t i = 0; i < Count.size(); i++){
  51. if (Count[i] == vec[i].size()) continue;
  52. if (M != vec[i][Count[i]].first){
  53. vec[i][Count[i]].second++;
  54. }
  55. else{
  56. Count[i]++;
  57. }
  58. }
  59. }
  60. }
  61. for (std::size_t i = 0; i < vec.size(); i++){
  62. for (std::size_t j = 0; j < vec[i].size(); j++){
  63. for (int k=0; k < vec[i][j].second; k++){
  64. std::cout << ' ';
  65. }
  66. std::cout << vec[i][j].first;
  67. }
  68. std::cout << std::endl;
  69. }
  70.  
  71. return true;
  72. }
  73.  
  74. int main(){
  75. std::string str{ "123\n134" };
  76. std::string str2{ "1212\n1322\n1122" };
  77.  
  78. makehoge(str);
  79. std::cout << std::endl;
  80. makehoge(str2);
  81. std::cout << std::endl;
  82. return 0;
  83. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
123
1 34

1 212
1    322
112 2