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 <string>
#include <iostream>
#include <map>
#include <cstdio>
 
using namespace std;
 
int main(){
    string s;
	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 >> s){
		int sum = 0;
		s += 'I';
		for(int i = 0; i < s.size()-1; i++){
			if(m[s[i+1]] > m[s[i]]) sum-=m[s[i]];
			else sum+=m[s[i]];
		}
		cout << sum << endl;
	}
 
 
    return 0;
}

C++: #include<iostream>
#include<map>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
main(){
  map<char,int> kioku;
  kioku['I']=1;
  kioku['V']=5;
  kioku['X']=10;
  kioku['L']=50;
  kioku['C']=100;
  kioku['D']=500;
  kioku['M']=1000;
  char tmp[1000];
  char c;
  int num=0;
  int before=0;
  while(cin>>tmp){
    for(int i=0;i<strlen(tmp);i++){
      if(i!=strlen(tmp)-1){
	if(kioku[tmp[i+1]]>kioku[tmp[i]]){
	  num+=abs(kioku[tmp[i]]-kioku[tmp[i+1]]);
	  i++;
	}
	else{
	  num+=kioku[tmp[i]];
	}
      }
      else{
	num+=kioku[tmp[i]];
      }
    }
    cout<<num<<endl;
    num=0;
  }
}

C++: #include<iostream>
#include<string>
#include<cmath>
using namespace std;
int dight(int n){
  return n==0?1:int(log10(n))+1;
}
int dif(int a,int b){
  if(a<b) return b-a;
  else return a+b;
}
int m(char c){
  if(c=='I') return 1;
  else if(c=='V') return 5;
  else if(c=='X') return 10;
  else if(c=='L') return 50;
  else if(c=='C') return 100;
  else if(c=='D') return 500;
  else return 1000;
}
int main(){
  string s;
  while(cin>>s){
    int ans = 0,a[100],count=0;
    a[0] = m(s[0]);
    for(int i=1;i<s.size();i++){
      if(s[i]==s[i-1]) a[count] += m(s[i]);
      else{
	count++;
	a[count] = m(s[i]);
      }
    }
    for(int i=1;i<=count;i++){
      if(a[i-1]>a[i]&&dight(a[i-1])>dight(a[i])) ans += a[i-1];
      else{
	ans += dif(a[i-1],a[i]);
	i++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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 roma2num(char c){
	return c=='I'? 1:
		c=='V'? 5:
		c=='X'? 10:
		c=='L'? 50:
		c=='C'? 100:
		c=='D'? 500: 1000;
}

int main(){
	string str;
	while(1){
		cin >> str;
		int sum=0;
		int i=0;
		for(;i<str.size()-1;i++){
			int now = roma2num(str[i]);
			int next = roma2num(str[i+1]);

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

		if(i < str.size()) sum += roma2num(str[i]);

		cout << sum << endl;

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

C++: #define _USE_MATH_DEFINES
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

int num(char c){
	switch(c){
	case 'I':
		return 1;
	case 'V':
		return 5;
	case 'X':
		return 10;
	case 'L':
		return 50;
	case 'C':
		return 100;
	case 'D':
		return 500;
	case 'M':
		return 1000;
	}

	return 0;
}


int main(){
	string str;
	while(cin >> str){
		int pre = num(str[0]);
		int sum = pre;
		//int sum = 0;
		for(int i=1;i<str.size();i++){
			if(pre>=num(str[i])){
				sum+=num(str[i]);
			}else{
				sum+=-pre+num(str[i])-pre;
			}
			pre = num(str[i]);
			//cout << sum <<" ";
		}
		cout<<sum<<endl;
	}
	return 0;
}

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 roma2num(char c){
	return c=='I'? 1:
		c=='V'? 5:
		c=='X'? 10:
		c=='L'? 50:
		c=='C'? 100:
		c=='D'? 500: 1000;
}

int main(){
	string str;
	while(1){
		cin >> str;
		int sum=0;
		int i=0;
		for(;i<str.size()-1;i++){
			int now = roma2num(str[i]);
			int next = roma2num(str[i+1]);

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

		if(i < str.size()) sum += roma2num(str[i]);

		cout << sum << endl;
		str.clear();

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

C++: #include <iostream>
#include <string>
using namespace std;
int main()
{
    int dic[26];
    for (int i=0; i<26; ++i) dic[i] = 0;
    dic['I'-'A'] = 1;
    dic['V'-'A'] = 5;
    dic['X'-'A'] = 10;
    dic['L'-'A'] = 50;
    dic['C'-'A'] = 100;
    dic['D'-'A'] = 500;
    dic['M'-'A'] = 1000;
    string s;
    while (cin >> s) {
        int res = 0;
        for (int i=0; i<s.size()-1; ++i) {
            if (dic[s[i+1]-'A'] > dic[s[i]-'A'])
                res -= dic[s[i]-'A'];
            else
                res += dic[s[i]-'A'];
        }
        res += dic[s[s.size()-1]-'A'];
        cout << res << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	string str;
	int l, s, t;
	
	while(cin >> str){
		vector<int> vec;
		l = str.size();
		
		for(int i = 0; i < l; i++){
			if(str[i] == 'M'){
				vec.push_back(1000);
			} else if(str[i] == 'D'){
				vec.push_back(500);
			} else if(str[i] == 'C'){
				vec.push_back(100);
			} else if(str[i] == 'X'){
				if(str[i+1] == 'C'){
					vec.push_back(90);
					i++;
				} else if(str[i+1] == 'L'){
					vec.push_back(40);
					i++;
				} else if(str[i+1] == 'V'){
					t = 15;
					i++;
					s = 1;
					while(str[i+s] == 'I' && s <= 3 && i+s < l){
						t += 1;
						s++;
					}
					i += (s-1);
					vec.push_back(t);
				} else if(str[i+1] == 'I'){
					i++;
					if(str[i+1] == 'V'){
						i++;
						vec.push_back(14);
					} else if(str[i+1] == 'X'){
						i++;
						vec.push_back(19);
					} else {
						t = 11;
						s = 1;
						while(str[i+s] == 'I' && s <= 2 && i+s < l){
							s++;
							t++;
						}
						i += (s-1);
						vec.push_back(t);
					}
				} else {
					t = 10;
					s = 1;
					while(str[i+s] == 'X' && s <= 2 && i+s < l){
						t += 10;
						s++;
					}
					i += (s-1);
					vec.push_back(t);
				}
			} else if(str[i] == 'L'){
				t = 50;
				s = 1;
				while(str[i+s] == 'X' && s <= 3 && i+s < l){
					t += 10;
					s++;
				}
				i += (s-1);
				vec.push_back(t);
			} else if(str[i] == 'V'){
				t = 5;
				s = 1;
				while(str[i+s] == 'I' && s <= 3 && i+s < l){
					t += 1;
					s++;
				}
				i += (s-1);
				vec.push_back(t);
			} else {
				if(str[i+1] == 'V'){
					i++;
					vec.push_back(4);
				} else if(str[i+1] == 'X'){
					i++;
					vec.push_back(9);
				} else {
					t = 1;
					while(str[i+t] == 'I' && t < 3 && i+t < l){
						t++;
					}
					i += (t-1);
					vec.push_back(t);
				}
			}
		}
		
		t = 0;
		for(int i = 0; i < vec.size(); i++){
			t += vec[i];
		}
		
		for(int i = 0; i < vec.size()-1; i++){
			if(vec[i] < vec[i+1]){
				t -= 2*vec[i];
			}
		}
		
		cout << t << endl;
	}
	
	return 0;
}

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

int c2i[128];

void init(){
  c2i['I'] = 1;
  c2i['V'] = 5;
  c2i['X'] = 10;
  c2i['L'] = 50;
  c2i['C'] = 100;
  c2i['D'] = 500;
  c2i['M'] = 1000;
}

int main() {
  string str;

  init();
  
  while(cin >> str) {
    int n = str.size();
    int sum = 0;
    for(int i = 0; i < n-1; i++) {
      sum += c2i[str[i]] * ( (c2i[str[i]] < c2i[str[i+1]]) ? -1 : 1);
    }
    sum += c2i[str[n-1]];
    
    cout << sum << endl;
  }

  return 0;
}