Roman numeral converter function Create the shortest function to convert a string of Roman numerals to an integer.

The rules for each letter can be found at the Wikipedia page.  Letters above 1,000 will have parentheses placed around them to signal their higher value.

Requirements:


Must convert Roman numerals 1 to 500,000
Must complete in less than a minute
Does not use built-in functions that could provide an advantage (Ex: A function that converts Roman numerals to integers)
Is a function


The function does not need to support fractions.  Any invalid input should return the number 0.

Shortest function wins.  In the case of a tie, the one with the most votes wins.

Test Cases

Input

III


Output

3



Input

IIII


Output

0



Input

XVI


Output

16



Input

(C)(D)(L)MMI


Output

452001
C++: 914 855 chars
#include&lt;map&gt;
#include&lt;string&gt;
#include&lt;iostream&gt;
#include&lt;sstream&gt;
#define I istream
#define T(C) if(C)throw int(1);
#define X(c,v,f,m) D[c]=v;P[c]=D[f];M[c]=m;
#define S second
using namespace std;typedef map&lt;char,int&gt;R;R D,P,M;struct U{U():t(0),l(0),a(0){}int t,l,a;operator int(){return t+l;}I&amp;d(I&amp;s){char c,b;s&gt;&gt;c;if(c=='('){s&gt;&gt;c&gt;&gt;b;T(b!=')')c+=32;}if(s){R::iterator f=D.find(c);T(f==D.end())if(P[c]==l){l=f-&gt;S-l;a=0;}else{T(l&amp;&amp;(f-&gt;S&gt;l))a=l==f-&gt;S?a+1:1;T(a&gt;M[c])t+=l;l=f-&gt;S;}}return s;}};I&amp;operator&gt;&gt;(I&amp;s,U&amp;d){return d.d(s);}int main(){D[' ']=-1;X(73,1,32,3)X(86,5,73,1)X(88,10,73,3)X(76,50,88,1)X(67,100,88,3)X(68,500,67,1)X(77,1000,67,3)X(118,5000,77,1)X(120,10000,77,3)X(108,50000,120,1)X(99,100000,120,3)X(100,500000,99,1)X(109,1000000,99,3)string w;while(cin&gt;&gt;w){try{stringstream s(w);U c;while(s&gt;&gt;c);cout&lt;&lt;c&lt;&lt;&quot;\n&quot;;}catch(int x){cout&lt;&lt;&quot;0\n&quot;;}}}

It could be compressed further.
&gt; ./a.exe
III
3
IIII
0
XVI
16
(C)(D)(L)MMI
452001

Slightly nicer formatting: 1582 char
#include&lt;map&gt;
#include&lt;string&gt;
#include&lt;iostream&gt;
#include&lt;sstream&gt;
#define I istream
#define T(C) if(C)throw int(1);
#define X(c,v,f,m) D[c]=v;P[c]=D[f];M[c]=m;
#define S second
using namespace std;

typedef map&lt;char,int&gt;      R;

R     D,P,M;

struct U
{
    U(): t(0), l(0), a(0) {}

    int  t,l,a;

    operator int()
    {
        return t + l;
    }
    I&amp; d(I&amp; s)
    {
        char c,b;
        s &gt;&gt; c;
        if (c == '(')
        {
            s &gt;&gt; c &gt;&gt; b;
            T(b != ')')
            c = tolower(c);
        }
        if (s)
        {
            R::iterator f = D.find(c);
            T(f == D.end())

            if (P[c] == l)
            {
                l = f-&gt;S - l;
                a = 0;
            }
            else
            {
                T(l&amp;&amp;(f-&gt;S &gt; l))
                a=l==f-&gt;S?a+1:1;
                T(a&gt;M[c])
                t   += l;
                l     = f-&gt;S;
            }
        }

        return s;
    }

};

I&amp; operator&gt;&gt;(I&amp; s,U&amp; d)
{
    return d.d(s);
}

int main()
{
    D[' ']=-1;
    X(73,1,32,3)
    X(86,5,73,1)
    X(88,10,73,3)
    X(76,50,88,1)
    X(67,100,88,3)
    X(68,500,67,1)
    X(77,1000,67,3)
    X(118,5000,77,1)
    X(120,10000,77,3)
    X(108,50000,120,1)
    X(99,100000,120,3)
    X(100,500000,99,1)
    X(109,1000000,99,3)

    string w;
    while(cin &gt;&gt; w)
    {
        try
        {
            stringstream s(w);
            U    c;
            while(s &gt;&gt; c);
            cout &lt;&lt; c &lt;&lt; &quot;\n&quot;;
        }
        catch(int x)
        {
            cout &lt;&lt; &quot;0\n&quot;;
        }
    }
}