Go: package main

import (
	"bufio"
	"os"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	dict := make(map[string]int)
	dict[" "] = 0
	dict["I"] = 1
	dict["V"] = 5
	dict["X"] = 10
	dict["L"] = 50
	dict["C"] = 100
	dict["D"] = 500
	dict["M"] = 1000
	for scanner.Scan() {
		line := strings.Split(scanner.Text(), "")
		line = append(line, " ")
		sum := 0
		for i := 0; i < len(line)-1; i++ {
			if dict[line[i]] < dict[line[i+1]] {
				sum -= dict[line[i]]
			} else {
				sum += dict[line[i]]
			}
		}
		fmt.Println(sum)
	}
}

C++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
const double PI = acos(-1);
const ll MOD = 1000000007;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
///////////////////////////////////////////////////////////////

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
	map<char, int> dic;
	dic['I'] = 1;
	dic['V'] = 5;
	dic['X'] = 10;
	dic['L'] = 50;
	dic['C'] = 100;
	dic['D'] = 500;
	dic['M'] = 1000;
	while (true) {
		string S = ""; cin >> S;
		if (S == "") break;
		int N = S.size();
		int ans = 0;
		rep(i,N) {
			if (i != N-1) {
				if (dic[S[i]] < dic[S[i+1]]) ans -= dic[S[i]];
				else ans += dic[S[i]];
			} else ans += dic[S[i]];
		}
		cout << ans << endl;
	}

}

C++: #include <map>
#include <iostream>

using namespace std;

/** Problem0039 : Roman Figure **/
int main()
{
	char num[110]; int now, old, ans;
	map<char, int> list;
	list.insert(map<char, int>::value_type('I', 1));
	list.insert(map<char, int>::value_type('V', 5));
	list.insert(map<char, int>::value_type('X', 10));
	list.insert(map<char, int>::value_type('L', 50));
	list.insert(map<char, int>::value_type('C', 100));
	list.insert(map<char, int>::value_type('D', 500));
	list.insert(map<char, int>::value_type('M', 1000));
	
	while (cin >> num) {
		ans = 0;
		old = list[num[0]];
		for (int i=1; i<110 && num[i]!='\0'; i++) {
			now = list[num[i]];
			if (old == 0) {
				old = now;
			} else if (old<now) {
				ans += (now-old);
				old = 0;
			} else {
				ans += old;
				old = now;
			}
		}
		ans += old;
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

int ans;
string str;


int main() {
	while ( ( cin >> str ) ) {
		ans = 0;
		for ( int i = 0; i < str.size(); i++ ) {
			if ( str[ i ] == 'M' ) {
				if ( ans % 1000 ) {
					ans += 800;
				}
				else {
					ans += 1000;
				}
			}
			else if ( str[ i ] == 'D' ) {
				if ( ans % 500 ) {
					ans += 300;
				}
				else {
					ans += 500;
				}
			}
			else if ( str[ i ] == 'C' ) {
				if ( ans % 100 ) {
					ans += 80;
				}
				else {
					ans += 100;
				}
			}
			else if ( str[ i ] == 'L' ) {
				if ( ans % 50 ) {
					ans += 30;
				}
				else {
					ans += 50;
				}
			}
			else if ( str[ i ] == 'X' ) {
				if ( ans % 10 ) {
					ans += 8;
				}
				else {
					ans += 10;
				}
			}
			else if ( str[ i ] == 'V' ) {
				if ( ans % 5 ) {
					ans += 3;
				}
				else {
					ans += 5;
				}
			}
			else {
				ans++;
			}
		}
		printf( "%d\n", ans );
	}
	return 0;
}

C++: #include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int main(){
    string str;
    int ans=0;
    map<char,int> M;
    M['I']=1;M['V']=5;M['X']=10;M['L']=50;M['C']=100;M['D']=500;M['M']=1000;
    while(cin>>str){
        for(int i=0;i<str.size();i++){
            if(str.size()==i-1)ans+=M[str[i]];
            else if(M[str[i]]<M[str[i+1]])ans-=M[str[i]];
            else ans+=M[str[i]];
        }
        cout<<ans<<endl;
    }
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>

typedef long long ll;
using namespace std;

char name[] = {'I','V','X','L','C','D','M'};
int num[] = {1,5,10,50,100,500,1000};

int main(){
	string str;
	while(1){
		cin >> str;
		int sum=0;
		for(int i=0;i<str.size();i++){
			char* pos;
			if((pos = find(name,name+7,str[i])) != name+7){
				int next;
				int now = num[pos-name];

				char* nextpos;
				if(i != str.size() - 1
					&& (nextpos = find(name,name+7,str[i+1])) != name+7){
					next = num[nextpos-name];
				}else{
					next = -1;
				}

				if(now >= next){
					sum += now;
				}else{
					sum += next - now;
					i++;
				}
			}
		}
		cout << sum << endl;

		if(cin.eof()) break;
	}
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;

#define pb         push_back
#define mp         make_pair
#define ll         long long
#define PI         acos(-1.0)
#define ALL(A)     ((A).begin(), (A).end())
#define vsort(v)   sort(v.begin(),v.end())
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)

int main(){
    string s;
    map<char, int> arab;
    arab['I'] = 1;
    arab['V'] = 5;
    arab['X'] = 10;
    arab['L'] = 50;
    arab['C'] = 100;
    arab['D'] = 500;
    arab['M'] = 1000;
    
    while(getline(cin, s)){
        int ans = 0;
        ans += arab[s[s.length()-1]];
        for(int i=s.length()-1; i>=1; i--){
            if(arab[s[i-1]] < arab[s[i]]) ans -= arab[s[i-1]];
            else ans += arab[s[i-1]];
        }
        cout << ans << endl;
    }
}