#include #include #include using namespace std; const int I = 1; const int V = 5; const int X = 10; const int L = 50; const int C = 100; const int D = 500; const int M = 1000; int parsechar(char c) { switch (c) { case 'i': return I; case 'v': return V; case 'x': return X; case 'l': return L; case 'c': return C; case 'd': return D; case 'm': return M; default: return 0; } } long parseroman(string s) { long out = 0; for (int i = 0; i < s.size(); i++) out += parsechar(s[i]); return out; } string parse(string s) { string out = ""; for (unsigned int i = 0; i < s.size(); i++) { char c = s[i]; if ((c == 'm') || (c == 'd') || (c == 'c') || (c == 'l') || (c == 'x') || (c == 'v') || (c == 'i')) out += c; } return out; } int findnext(int pos, string s, char c) { pos++; while ((pos < s.size()) && (s[pos] != c)) pos++; if (pos >= s.size()) return -1; else return pos; } int main() { string line; char *c=NULL; size_t delka = 10000; while (getline(&c, &delka, stdin) != -1) { line = c; string rline = parse(line); string roman = ""; int pos = -1; bool out = false; while (!out) { int pom = findnext(pos, rline, 'm'); if (pom != -1) { roman += 'm'; pos = pom; } else out = true; } out = false; int count = 0; while (!out) { int pom = findnext(pos, rline, 'd'); if (pom != -1) { count ++; if (count <= 1) roman += 'd'; pos = pom; } else out = true; } out = false; count = 0; while (!out) { int pom = findnext(pos, rline, 'c'); if (pom != -1) { count ++; if (count <= 3) roman += 'c'; pos = pom; } else out = true; } out = false; count = 0; while (!out) { int pom = findnext(pos, rline, 'l'); if (pom != -1) { count ++; if (count <= 1) roman += 'l'; pos = pom; } else out = true; } out = false; count = 0; while (!out) { int pom = findnext(pos, rline, 'x'); if (pom != -1) { count ++; if (count <= 3) roman += 'x'; pos = pom; } else out = true; } out = false; count = 0; while (!out) { int pom = findnext(pos, rline, 'v'); if (pom != -1) { count ++; if (count <= 1) roman += 'v'; pos = pom; } else out = true; } out = false; count = 0; while (!out) { int pom = findnext(pos, rline, 'i'); if (pom != -1) { count ++; if (count <= 3) roman += 'i'; pos = pom; } else out = true; } cout << parseroman(roman) << endl; } }