#include #include using namespace std; #define M 0 #define D 1 #define C 2 #define L 3 #define X 4 #define V 5 #define I 6 int value[] ={ 1000, 500, 100, 50, 10, 5, 1 }; int rep[] = {-1,1,3,1,3,1,3}; int conv(char z) { switch (z) { case 'm': return M; case 'd': return D; case 'c': return C; case 'l': return L; case 'x':return X; case 'v': return V; case 'i': return I; } return -1; } struct node { int v; node *next[7]; }; node root; int test(int z, int z2) { if (z == D && z2 == M) return -1; if (z == C && z2 == M) return -1; if (z == L && (z2 == M || z2 == D || z2 == C)) return -1; if (z == X && (z2 == M || z2 == D)) return -1; if (z == V && (z2 == M || z2 == D || z2 == C)) return -1; if (z == V && (z2 == L || z2 == X)) return -1; if (z == I && (z2 == M || z2 == D || z2 == C)) return -1; if (z == I && (z2 == L)) return -1; return 0; } int num(string s) { int t; int v = 0; int z,z2; int l = (int)s.length()-1; char p = -1; int num; for (t=0;t<(l+1);t++) { z = conv(s[t]); if (z !=p) { num = 1; p = z; } else { num++; if (rep[z] == -1) continue; if (rep[z] < num) return -1; } } t = 0; while (t<=l) { z = conv(s[t]); if (t!=l) { z2 = conv(s[t+1]); if (test(z,z2) == -1) return -1; if (z>z2) { if (t!=0) { int a; a = conv(s[t-1]); if(a==z) return -1; } v += (value[z2]-value[z]); if ((t+1)!=l) { z =z2; z2 = conv(s[t+2]); if (test(z,z2) == -1) return -1; } t+=2; } else { v += value[z]; t++; } } else { v += value[z]; t++; } } return v; } string get_string(string s) { int t; string x; for (t=0;t<(int)s.length();t++) { if (conv(s[t]) != -1) { x += s[t]; } } return x; } long int getb(long int x,int m) { long int msk = (1 << m); return (x & msk); } void init_node(node *n) { n->v = -2; int t; for (t=0;t<7;t++) { n->next[t] = NULL; } } void compute(string s) { long int c=1; long int t; int l = (int)s.length(); char m[1000]; int max = 0; for (t=0;tnext[v] == NULL) { found = false; n->next[v] = new node; init_node(n->next[v]); } n = n->next[v]; hn++; } int zzz; if (found) { if (n->v == -2) { zzz = num(m); n->v = zzz; } else { zzz = n->v; } } else { zzz = num(m); n->v = zzz; } // cout << t << " " << m << " " << num(m) << endl; if (max < zzz) max = zzz; } cout << max << endl; } string readline() { string s; char c; c = getc(stdin); while (c != '\n'){ s += c; c = getc(stdin); } return s; } int main() { /* string s = get_string("metfyz is the best school in prague"); cout << s<< endl; compute(s);*/ /* string s = get_string("no year"); cout << s<< endl; compute(s);*/ string s; init_node(&root); s = readline(); while (s != "") { compute(get_string(s)); // cout << s << endl; s = readline(); } /* printf("%i\n",num("CII")); printf("%i\n",num("XCIX")); printf("%i\n",num("CXX")); printf("%i\n",num("LII")); printf("%i\n",num("IX")); printf("%i\n",num("VII")); printf("%i\n",num("MMMCDXLIX")); printf("%i\n",num("IC")); printf("%i\n",num("IIC")); printf("%i\n",num("VX")); printf("%i\n",num("IM")); printf("%i\n",num("MIIII")); printf("%i\n",num("MDD")); printf("%i\n",num("XVV")); printf("%i\n",num("IXM")); printf("%i\n",num("xcx")); */ }