fork download
  1. #include<iostream>
  2. #include<cmath>
  3. #include<algorithm>
  4. #include<iomanip>
  5. #include<climits>
  6. #include<numeric>
  7. #include<set>
  8. #include<sstream>
  9. #include<map>
  10. #include<vector>
  11. #include<string>
  12. #include<regex>
  13.  
  14. using namespace std;
  15.  
  16. const long long MOD = (long long)(1e9 + 7);
  17.  
  18. vector<string> split(const string& haystack, const string& needle) {
  19. vector<string> result;
  20. size_t start_pos = 0;
  21. size_t found_pos = haystack.find(needle, start_pos);
  22. while (found_pos != string::npos) {
  23. size_t count = found_pos - start_pos;
  24. string token = haystack.substr(start_pos, count);
  25. if (!token.empty()) {
  26. result.push_back(token);
  27. }
  28. start_pos = found_pos + needle.length();
  29. found_pos = haystack.find(needle, start_pos);
  30. }
  31.  
  32. string last_token = haystack.substr(start_pos);
  33. if (!last_token.empty()) {
  34. result.push_back(last_token);
  35. }
  36. return result;
  37. }
  38.  
  39. int main() {
  40. int n;cin >> n;
  41. cin.ignore();
  42. for (int i = 1;i <= n;i++) {
  43. map<int, int> result;
  44. string line;getline(cin, line);
  45. auto parts = split(line, " + ");
  46. for (auto part : parts) {
  47. auto value = split(part, "x^");
  48. result[stoi(value[1])] += stoi(value[0]);
  49. }
  50.  
  51. for (auto entry = result.begin();entry != result.end();entry++) {
  52. cout << entry->second << "x^" << entry->first;
  53. if (entry != (--result.end())) {
  54. cout << " + ";
  55. }
  56. }
  57. cout << endl;
  58. }
  59. return 0;
  60. }
  61.  
  62.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty