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 <iostream>
#include <map>
using namespace std;

map<char, int> create_map() {
    map<char, int> m;
    m.insert({'I', 1});
    m.insert({'V', 5});
    m.insert({'X', 10});
    m.insert({'L', 50});
    m.insert({'C', 100});
    m.insert({'D', 500});
    m.insert({'M', 1000});
    return m;
}
map<char, int> m = create_map();
string str;
int idx;

int setN(char i, char j, char k) {
    int num = 0;
    if (idx >= 1 && (str[idx] == k || str[idx] == j) && str[idx-1] == i) { num = m[str[idx]] - m[str[idx-1]]; idx-=2; }
    else if (str[idx] == i) {
        for (; ;) {
            if (idx < 0) break;
            if (str[idx] != i) break;
            num+= m[str[idx--]];
        }
    }
    if (idx >= 0 && str[idx] == j) { num += m[str[idx--]]; }
    return num;
}


int main() {
    while(cin>>str) {
        int n = 0;
        idx = int(str.size()-1);
        n = setN('I', 'V', 'X');
        if (idx >= 0) n+= setN('X', 'L', 'C');
        if (idx >= 0) n+= setN('C', 'D', 'M');
        if (idx >= 0) for (; ;) { if (idx < 0) break; n+= m[str[idx--]]; }
        cout << n << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;

int toNum(char a){
	if (a == 'I')		return 1;
	else if (a == 'V')	return 5;
	else if (a == 'X')	return 10;
	else if (a == 'L')	return 50;
	else if (a == 'C')	return 100;
	else if (a == 'D')	return 500;
	else if (a == 'M')	return 1000;
	else return 0;
}

int solve(string in){
	int ans = 0;
	for (int i = 0; i < in.length(); i++){
		int first = toNum(in[i]);
		int second = -1;
		if (i < in.length() - 1){
			second = toNum(in[i + 1]);
		}
		if (first < second){
			ans += (second - first);
			i++;
		}
		else{
			ans += first;
		}
	}

	return ans;
}

int main(){
	string in;

	while (cin >> in, !cin.eof()){
		cout << solve(in) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;
int num[255];

int main() {
    num['I'] = 1;
    num['V'] = 5;
    num['X'] = 10;
    num['L'] = 50;
    num['C'] = 100;
    num['D'] = 1000;
    string s;
    while (cin >> s) {
        int n = num[s.back()];
        for (int i=0; i<(int)s.size()-1; ++i) {
            if (num[s[i]] < num[s[i+1]]) {
                n -= num[s[i]];
            } else {
                n += num[s[i]];
            }
        }
        cout << n << endl;
    }
    return 0;
}

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

int main(){
	string s;
	while(cin >> s){
		char b[s.size()];
		for(int i=0;i<s.size();i++) b[i] = s[i];
		int w = 0;
		for(int i=0;i<s.length();i++){
			if(b[i]=='M') w += 1000;
			if(b[i]=='D') w += 500;
			if(b[i]=='C') w += 100;
			if(b[i]=='L') w += 50;
			if(b[i]=='X') w += 10;
			if(b[i]=='V') w += 5;
			if(b[i]=='I') w += 1;
		}
		for(int i=0;i<s.length()-1;i++){
			if(b[i]=='I' and b[i+1]!='I') w -= 2;
			if(b[i]=='V' and (b[i+1]!='V' and b[i+1]!='I')) w -= 10;
			if(b[i]=='X' and (b[i+1]!='X' and b[i+1]!='V' and b[i+1]!='I')) w -= 20;
			if(b[i]=='L' and (b[i+1]=='C' or b[i+1]=='D' or b[i+1]=='M')) w -= 100;
			if(b[i]=='C' and (b[i+1]=='D' or b[i+1]=='M')) w -= 200;
			if(b[i]=='D' and b[i+1]=='D') w -= 500;
		}
		cout << w << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<vector>
using namespace std;
#define INF (1<<20)
map<string,int> m;
vector<int> num;
main(){
  m["I"]=1;
  m["II"]=2;
  m["III"]=3;
  m["IV"]=4;
  m["V"]=5;
  m["VI"]=6;
  m["VII"]=7;
  m["VIII"]=8;
  m["IX"]=9;
  m["X"]=10;

  m["XI"]=11;
  m["XII"]=12;
  m["XIII"]=13;
  m["XIV"]=14;
  m["XV"]=15;
  m["XVI"]=16;
  m["XVII"]=17;
  m["XVIII"]=18;
  m["XIX"]=19;
  m["XX"]=20;

  m["XXX"]=30;
  m["XL"]=40;
  m["L"]=50;
  m["LX"]=60;
  m["LXX"]=70;
  m["LXXX"]=80;
  m["XC"]=90;
  m["C"]=100;
  m["D"]=500;
  m["M"]=1000;
  string str;
  while(cin >> str){
    int res=0;
    int tmp;
    for(int i=0;i<(int)str.size();i++){
      for(int j=4;j>0;j--){
	if(i+j>str.size()) continue;
	if(m[str.substr(i,j)]>0){
	  num.push_back(m[str.substr(i,j)]);
	  i+=(j-1);
	  break;
	}
      }
    }
    int i=0;
    for(i=0;i<num.size()-1;i++){
      // cout << num[i] << endl;
      if(num[i]>=num[i+1]) res += num[i];
      else{ res += (num[i+1]-num[i]); i++;}
    }
    
    //   cout << num.back() << endl;
    if(i==num.size()-1)
      res += num.back();
    cout << res << endl;
    num.clear();
  }
}

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


int main(){
  int sum,i,j,kari;
  string data;
  while(getline(cin, data)) {
    data = data.insert(data.length(), "KKKKKK");
    sum = j = 0;
    
    for(j = 0; j < data.length(); j++) {
      if(data[j] == 'M'){
	sum += 1000;
      }else if(data[j] == 'D'){
	if(data[j + 1] == 'M'){
	  sum += 900;
	    j++;
	}else{
	  sum += 500;
	}
      }else if(data[j] == 'C'){
	if(data[j + 1] == 'D' || data[j + 1] == 'M'){
	  if(data[j + 1] == 'D'){
	    sum += 400;
	    j++;
	  }else{
	    sum += 900;
	    j++;
	  }
	}else{
	  sum += 100;
	}
      }else if(data[j] == 'L'){
	sum += 50;
      }else if(data[j] == 'X'){
	if(data[j+1] == 'L' || data[j+1] == 'C'){
	  if(data[j+1] == 'L'){
	    sum += 40;
	    j++;
	  }else{
	    sum += 90;
	    j++;
	  }
	}else{
	  sum += 10;
	}
      }else if(data[j] == 'V'){
	sum += 5;
      }else if(data[j] == 'I'){
	if(data[j + 1] == 'V' || data[j + 1] == 'X'){
	  if(data[j + 1] == 'V'){
	    sum += 4;
	    j++;
	  }else{
	    sum += 9;
	    j++;
	  }
	}
	else{
	  sum += 1;
	}
      }
    }
    
    cout << sum << endl;
  }
  
  return 0;
}

C++: #include<cstdio>
#include<cstring>

using namespace std;
int main(void)
{
	char r[100000];
	int kari[100000] = {0};
	while(scanf("%s",r) != EOF)
	{
		long long int ans = 0;
		int rs = strlen(r);
		for(int i = 0;i < rs;i++)
		{
			     if(r[i] == 'I') kari[i] = 1;
			else if(r[i] == 'V') kari[i] = 5;
			else if(r[i] == 'X') kari[i] = 10;
			else if(r[i] == 'L') kari[i] = 50;
			else if(r[i] == 'C') kari[i] = 100;
			else if(r[i] == 'D') kari[i] = 500;
			else if(r[i] == 'M') kari[i] = 1000;
		}

		for(int i = 0;i < rs;i++)
		{
			if(kari[i] < kari[i+1]) ans -= kari[i];
			else ans += kari[i];
		}
		printf("%lld\n",ans);
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int check(char x){
    if(x=='M')return 1000;
    else if(x=='D')return 500;
    else if(x=='C')return 100;
    else if(x=='L')return 50;
    else if(x=='X')return 10;
    else if(x=='V')return 5;
    else if(x=='I')return 1;
    else return 0;
}
int main(void){

	string str;

	while(cin>>str){
    int ans=0;
        for(int i=0;;i++){
            if(check(str[i])<check(str[i+1]))ans+=(check(str[i+1])-check(str[i])),i++;
            else if(str[i]==0)break;
            else ans+=check(str[i]);

        }

        cout<<ans<<endl;
        ans=0;

        str="";
	}


	return 0;
}