#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

string to_string(const vector<int>& poly)
{
  ostringstream os;
  size_t deg = poly.size();

  if(deg > 2)
  {
    --deg;
    os << poly[deg] << "x^" << deg;
  }

  os.setf(ios_base::showpos);

  while(deg-- > 2)
  {
    os << poly[deg] << "x^" << deg;
  }

  os << poly[deg--] << "x";
  os << poly[deg];

  return os.str();
}

vector<int> from_string(const string& str)
{
  istringstream is(str);
  vector<int> result;
  char c;
  int x = 0;
  while(is.get(c))
  {
    switch(c)
    {
      case '+':
      case '-':
        is.putback(c);
        is >> x;
        result.push_back(x);
      break;
      case '^':
        is >> x;
      break;
      default:
        if(isdigit(c))
        {
          is.putback(c);
          is >> x;
          result.push_back(x);
        }
      break;
    }
  }
  reverse(result.begin(), result.end());
  return result;
}

int main()
{
  vector<int> koef = {5, -5, 3};
  string s = to_string(koef);

  cout << s << endl;

  vector<int> result = from_string(s);

  for(int i : result)
  {
    cout << i << " ";
  }
}