fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. struct Event {
  12. string id;
  13. string title;
  14. string acronym;
  15. string projectCode;
  16. string threeDigitCode;
  17. string recordType;
  18. string parentId;
  19. };
  20.  
  21. // Pour parser une ligne CSV en tenant compte des guillemets échappés
  22. vector<string> parseCSVLine(const string& line) {
  23. vector<string> result;
  24. stringstream ss(line);
  25. string item;
  26. bool inQuotes = false;
  27. string currentField;
  28.  
  29. for (char ch : line) {
  30. if (ch == '"') {
  31. if (inQuotes && ss.peek() == '"') { // double quotes
  32. currentField += ch;
  33. ss.get();
  34. } else {
  35. inQuotes = !inQuotes;
  36. }
  37. } else if (ch == ',' && !inQuotes) {
  38. result.push_back(currentField);
  39. currentField.clear();
  40. } else {
  41. currentField += ch;
  42. }
  43. }
  44. result.push_back(currentField);
  45. return result;
  46. }
  47.  
  48. bool compareEvents(const Event& a, const Event& b) {
  49. if (a.acronym != b.acronym) return a.acronym < b.acronym;
  50. if (a.recordType != b.recordType) return a.recordType == "Parent Event";
  51. if (a.title != b.title) return a.title < b.title;
  52. return a.id < b.id;
  53. }
  54.  
  55. int main() {
  56. string line;
  57. vector<Event> events;
  58. map<string, vector<Event>> eventsByAcronym;
  59.  
  60. // Lire les données
  61. while (getline(cin, line)) {
  62. vector<string> fields = parseCSVLine(line);
  63. if (fields.size() >= 6) {
  64. Event e;
  65. e.id = fields[0];
  66. e.title = fields[1];
  67. e.acronym = fields[2];
  68. e.projectCode = fields[3];
  69. e.threeDigitCode = fields[4];
  70. e.recordType = fields[5];
  71. if (fields.size() > 6) e.parentId = fields[6];
  72.  
  73. if (!e.acronym.empty()) {
  74. events.push_back(e);
  75. eventsByAcronym[e.acronym].push_back(e);
  76. }
  77. }
  78. }
  79.  
  80. // Traiter les données
  81. vector<Event> validEvents;
  82. for (auto& pair : eventsByAcronym) {
  83. vector<Event>& group = pair.second;
  84.  
  85. // Compter les parents
  86. int parentCount = 0;
  87. Event* parent = nullptr;
  88. for (Event& e : group) {
  89. if (e.recordType == "Parent Event") {
  90. parentCount++;
  91. parent = &e;
  92. }
  93. }
  94.  
  95. // Vérifier les conditions de validité
  96. if (parentCount == 1) {
  97. set<string> childCodes;
  98. vector<Event> children;
  99.  
  100. for (Event& e : group) {
  101. if (e.recordType != "Parent Event") {
  102. children.push_back(e);
  103. if (!e.threeDigitCode.empty()) {
  104. childCodes.insert(e.threeDigitCode);
  105. }
  106. }
  107. }
  108.  
  109. if (!children.empty()) {
  110. // Mettre à jour le code à 3 chiffres du parent
  111. if (childCodes.size() == 1) {
  112. parent->threeDigitCode = *childCodes.begin();
  113. } else {
  114. parent->threeDigitCode = "???";
  115. }
  116.  
  117. // Ajouter le parent et ses enfants
  118. validEvents.push_back(*parent);
  119. for (Event& child : children) {
  120. child.parentId = parent->id;
  121. validEvents.push_back(child);
  122. }
  123. }
  124. }
  125. }
  126.  
  127. // Trier et afficher les résultats
  128. sort(validEvents.begin(), validEvents.end(), compareEvents);
  129.  
  130. for (const Event& e : validEvents) {
  131. cout << e.id << ",\"" << e.title << "\",\"" << e.acronym << "\","
  132. << e.projectCode << "," << e.threeDigitCode << ",\"" << e.recordType << "\"";
  133. if (!e.parentId.empty()) cout << "," << e.parentId;
  134. cout << "\n";
  135. }
  136.  
  137. return 0;
  138. }
Success #stdin #stdout 0.01s 5272KB
stdin
a094x00000EXXzC,"IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",,,"Parent Event"
a094x00002MGty5,"2023 IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",23X51,X51,"IEEE Event"
a094x00001IYX7e,"2022 IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",22X51,X51,"IEEE Event"
a094x00000EXXrU,"Antennas Design and Measurement International Conference","acsos",,,"Parent Event"
a094x00000Bm112,"2021 Antennas Design and Measurement International Conference","acsos",21U43,U43,"IEEE Event"
a094x00000Bm0VM,"2019 Antennas Design and Measurement International Conference","acsos",19U43,U43,"IEEE Event"
a094x00000EXXzCxxx,"IEEE US Conference on Autonomic Computing and Self-Organizing Systems","1ACSOS",,,"Parent Event"
a094x00002MGty5xxx,"2023 IEEE US Conference on Autonomic Computing and Self-Organizing Systems","1ACSOS",23P00,P00,"IEEE Event"
a094x00000EXXzCyyy,"IEEE Conference on Autonomic Computing","bACSOS",,,"Parent Event"
a094x00002MGty5yyy,"2023 IEEE Conference on Autonomic Computing","bACSOS",23YN0,YN0,"IEEE Event"
a094x00000EXXrUxxx,"Antennas Design and Measurement US Conference","XYZ",,,"Parent Event"
stdout
a094x00000EXXzCxxx,"IEEE US Conference on Autonomic Computing and Self-Organizing Systems","1ACSOS",,P00,"Parent Event"
a094x00002MGty5xxx,"2023 IEEE US Conference on Autonomic Computing and Self-Organizing Systems","1ACSOS",23P00,P00,"IEEE Event",a094x00000EXXzCxxx
a094x00000EXXzC,"IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",,X51,"Parent Event"
a094x00001IYX7e,"2022 IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",22X51,X51,"IEEE Event",a094x00000EXXzC
a094x00002MGty5,"2023 IEEE International Conference on Autonomic Computing and Self-Organizing Systems","ACSOS",23X51,X51,"IEEE Event",a094x00000EXXzC
a094x00000EXXrU,"Antennas Design and Measurement International Conference","acsos",,U43,"Parent Event"
a094x00000Bm0VM,"2019 Antennas Design and Measurement International Conference","acsos",19U43,U43,"IEEE Event",a094x00000EXXrU
a094x00000Bm112,"2021 Antennas Design and Measurement International Conference","acsos",21U43,U43,"IEEE Event",a094x00000EXXrU
a094x00000EXXzCyyy,"IEEE Conference on Autonomic Computing","bACSOS",,YN0,"Parent Event"
a094x00002MGty5yyy,"2023 IEEE Conference on Autonomic Computing","bACSOS",23YN0,YN0,"IEEE Event",a094x00000EXXzCyyy