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++: #include <bits/stdc++.h>
  
using namespace std;
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)
  
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
  
#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

double dot(P a,P b){return real(conj(a)*b);}
double cross(P a,P b){return imag(conj(a)*b);}

int main(){
  int a[255];
  a['I'] = 1;
  a['V'] = 5;
  a['X'] = 10;
  a['L'] = 50;
  a['C'] = 100;
  a['D'] = 500;
  a['M'] = 1000;
  while(cin){
    string s;
    cin>>s;
    int bef = 114514;
    int res = 0;
    REP(i,s.size()){
      int now = a[s[i]];
      if(now > bef) res -= bef*2;
      res += now;
      bef = now;
    }
    cout<<res<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define SORT(a) sort(a.begin(), a.end())
#define RSORT(a) sort(a.rbegin(), a.rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, start, end) for (int i = start; i < end; i++)
#define ALL(a) a.begin(), a.end()
#define MOD(a) a %= 1'000'000'007
using ll = long long;
using namespace std;
const int INF32       = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7        = 1'000'000'007;
const int MOD9        = 1'000'000'009;
template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T> T roma_to_arabic(string s) {
    auto number = [](char c) -> int {
        if (c == 'I')
            return 1;
        if (c == 'V')
            return 5;
        if (c == 'X')
            return 10;
        if (c == 'L')
            return 50;
        if (c == 'C')
            return 100;
        if (c == 'D')
            return 500;
        if (c == 'M')
            return 1000;
    };
    T ans   = number(s[0]);
    int pre = number(s[0]);
    for (int i = 1; i < s.size(); i++) {
        int now = number(s[i]);
        now <= pre ? ans += now : ans += now - 2 * pre;
        pre = now;
    }
    return ans;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    string s;
    while (getline(cin, s)) {
        std::cout << roma_to_arabic<int>(s) << '\n';
    }

    return 0;
}

C++: #include <iostream>
using namespace std;

int main(){
    string ss;
    int num[128];
    num['M'] = 1000;
    num['D'] = 500;
    num['C'] = 100;
    num['L'] = 50;
    num['X'] = 10;
    num['V'] = 5;
    num['I'] = 1;
    while( cin >> ss ){
        string::iterator it = ss.begin();
        int answer = 0;
        while( it != ss.end() ){
            if( *it == 'C' && *(it+1) == 'D' ){
                answer += 400; it++;
            }else if( *it == 'X' && *(it+1) == 'C' ){
                answer += 90; it++;
            }else if( *it == 'X' && *(it+1) == 'L' ){
                answer += 40; it++;
            }else if( *it == 'I' && *(it+1) == 'X' ){
                answer += 9; it++;
            }else if( *it == 'I' && *(it+1) == 'V' ){
                answer += 4; it++;
            }else{
                answer += num[*it];
            }
            it++;
        }
        cout << answer << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> P;

const string ord = "IVXLCDM";
vector<P> v;

void init() {
    v.push_back(P(2, 1));

    for (int i = 1; i < ord.size(); i++) {
        if (i & 1) v.push_back(P(v[i - 1].first + 1, 5 * v[i - 1].second));
        else v.push_back(P(2 * v[i - 2].first, 2 * v[i - 1].second));
    }
}

int main() {
    init();

    string input;
    while (cin >> input) {
        input += "?";

        int ans = 0;
        int sz = input.size();
        for (int i = 1; i < sz; i++) {
            int pre = ord.find(input[i - 1]), now = ord.find(input[i]);

            if (v[now].first - v[pre].first == 1 || v[now].first / v[pre].first == 2) {
                ans -= v[pre].second;
            } else {
                ans += v[pre].second;
            }
        }
        
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <stack>
#include <iomanip>
#include <numeric>
#include <queue>
#include <climits>
#include <complex>
#include <cmath>
#include <map>
using namespace std;
using ll = long long;
#define MOD 1000000007
#define INF 1LL << 59

vector<string> v;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    //cout << fixed << setprecision(3);
    map<char, int> mp;
    mp['M'] = 1000;
    mp['D'] = 500;
    mp['C'] = 100;
    mp['L'] = 50;
    mp['X'] = 10;
    mp['V'] = 5;
    mp['I'] = 1;
    string s;
    while (cin >> s)
    {
        int n = s.size();
        int sum = mp[s[n - 1]];
        for (int i = n - 1; i >= 1; --i)
        {
            if (mp[s[i]] > mp[s[i - 1]])
            {
                sum -= mp[s[i - 1]];
            }
            else
            {
                sum += mp[s[i - 1]];
            }
        }
        cout << sum << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;

int To_num(char);
int Calculate(string);

int main(void){
  string roma;

  while(cin >> roma) cout << Calculate(roma) << endl;
  return 0;
}

int Calculate(string str)
{
  int ans = 0;
  int now;
  int next;
  
  for(int i = 0 ; i < str.size() ; i++)
    {
      now = To_num(str[i]);
      next = To_num(str[i+1]);
      
      if(now >= next) ans += now;
      else
	{
	  ans += next - now;
	  i++;
	}
    }
  return ans;
}
      
int To_num(char c)
{
  if(c == 'I') return 1;
  if(c == 'V') return 5;
  if(c == 'X') return 10;
  if(c == 'L') return 50;
  if(c == 'C') return 100;
  if(c == 'D') return 500;
  if(c == 'M') return 1000;
}

C++: #include <stdio.h>

int main()
{
	int i, j, x, num[100];
	char str[100];

	while(gets(str)){
		i = 0;
		j = 0;

		while(str[i] != '\0'){
			switch(str[i]){
				case 'I':
					x = 1;
					break;
				case 'V':
					x = 5;
					break;
				case 'X':
					x = 10;
					break;
				case 'L':
					x = 50;
					break;
				case 'C':
					x = 100;
					break;
				case 'D':
					x = 500;
					break;
				case 'M':
					x = 1000;
					break;
				default:
					return 0;
			}

			if(i == 0) num[j] = x;
			else if(str[i] == str[i-1]) num[j] += x;
			else {j++; num[j] = x;}

			i++;
		}

		i = 0;
		x = 0;
		while(num[i] != '\0'){
			if(num[i] < num[i+1]) x -= num[i];
			else x += num[i];

			i++;
		}

		printf("%d\n", x);
	}

	return 0;
}