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 <cstdio>
#include <iostream>
using namespace std;
int rtos(string str) {
    int num=0;
    /* I=1 V=5 X=10 L=50 C=100 D=500 M=1000 */
    for (int i=0; i<str.length(); i++) {
        switch (str[i]) {
            case 'I':
                num++;
                break;
            case 'V':
                num+=5;
                if (i&&str[i-1]=='I') num-=2;
                break;
            case 'X':
                num+=10;
                if (i&&str[i-1]=='I') num-=2;
                if (i&&str[i-1]=='V') num-=10;
                break;
            case 'L':
                num+=50;
                if (i&&str[i-1]=='I') num-=2;
                if (i&&str[i-1]=='V') num-=10;
                if (i&&str[i-1]=='X') num-=20;
                break;
            case 'C':
                num+=100;
                if (i&&str[i-1]=='I') num-=2;
                if (i&&str[i-1]=='V') num-=10;
                if (i&&str[i-1]=='X') num-=20;
                if (i&&str[i-1]=='L') num-=100;
                break;
            case 'D':
                num+=500;
                if (i&&str[i-1]=='I') num-=2;
                if (i&&str[i-1]=='V') num-=10;
                if (i&&str[i-1]=='X') num-=20;
                if (i&&str[i-1]=='L') num-=100;
                if (i&&str[i-1]=='C') num-=200;
                break;
            case 'M':
                num+=1000;
                if (i&&str[i-1]=='I') num-=2;
                if (i&&str[i-1]=='V') num-=10;
                if (i&&str[i-1]=='X') num-=20;
                if (i&&str[i-1]=='L') num-=100;
                if (i&&str[i-1]=='C') num-=200;
                if (i&&str[i-1]=='D') num-=1000;
                break;
        }
    }
    return num;
}
int main() {
    string roman;
    while (cin>>roman) {
        printf("%d\n",rtos(roman));
    }
}

C++: import java.util.Scanner;

//Roman Figure
public class AOJ0039 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			char[] s = sc.next().toCharArray();
			int sum = 0;
			int i;
			for(i=0;i<s.length-1;i++){
				if(f(s[i])>=f(s[i+1]))sum+=f(s[i]);
				else{
					sum += f(s[i+1])-f(s[i]);
					i++;
				}
			}
			if(i<s.length)sum += f(s[i]);
			System.out.println(sum);
		}
	}
	
	public static int f(char c){
		return c=='I'?1:
			c=='V'?5:
			c=='X'?10:
			c=='L'?50:
			c=='C'?100:
			c=='D'?500:1000;
	}
}

C++: #include <map>
#include <string>
#include <iostream>
using namespace std;
int main(){
	int i,r;
	string s;
	map<char,int>m;
	for(m['I']=1,m['V']=5,m['X']=10,m['L']=50,m['C']=100,m['D']=500,m['M']=1000;getline(cin,s);cout<<r<<endl)for(i=r=0;i<s.size();i++)r+=m[s[i]]*(m[s[i]]<m[s[i+1]]?-1:1);
}

C++: #include <bits/stdc++.h>
using namespace std;
//    std::sort(a.begin(), a.end(), std::greater<int>() );
#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG true

/*
std::vector<std::string> split(const std::string &input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter))
    {
        result.push_back(field);
    }
    return result;
}
*/

int main()
{
    string a;
    while (cin >> a)
    {

        int ans = 0;
        string b = "IVXLCDM";
        int c[] = {1, 5, 10, 50, 100, 500, 1000};
        int pre = 0;
        rep(i, a.size())
        {
            rep(j, b.size()) if (a[i] == b[j])
            {
                if (c[j] <= pre)
                {
                    ans += pre;
                    pre = c[j];
                }
                else
                {
                    ans -= pre;
                    pre = c[j];
                }
                break;
            }
        }
        ans += pre;
        cout << ans << endl;
        if (cin.eof())
        break;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
#include<list>
using namespace std;
int inf = 1000000000;


int main(void) {

  string alp = "IVXLCDM";
  int num[7] = {1, 5, 10, 50, 100, 500, 1000};

  string s;
  while(cin >> s){
    reverse(s.begin(), s.end());
    int n = 0;
    int b = 0;
    for(int i=0; i<s.size(); i++){
      for(int j=0; j<7; j++){
        if( s[i] == alp[j] ){
          // cerr << n << " " << b << endl;
          if( num[j] < b ) n -= num[j];
          else n += num[j];

          b = num[j];
        }
      }

    }
    cout << n << endl;

  }

  return 0;
}

// EOF

C++: #include <iostream>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stdio.h>
#include <string>
#include <string.h>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main (void)
{
	string a;

	int f[101];

	while (cin >> a)
	{
		int k = 0,co = 0;
		k = a.size();
		for (int i = 0; i < k; i++)
		{
			string p;
			p = a.substr(i,1);

			if (p == "C")
			{
				f[i] = 100;
			}
			else if (p == "L")
			{
				f[i] = 50;
			}
			else if (p == "D")
			{
				f[i] = 500;
			}
			else if (p == "M")
			{
				f[i] = 1000;
			}
			else if (p == "X")
			{
				f[i] = 10;
			}
			else if (p == "V")
			{
				f[i] = 5;
			}
			else
			{
				f[i] = 1;
			}
		}
		f[k] = 0;

		for (int i = 0; i < k; i++)
		{
			if (f[i] != 0)
			{
				if (f[i] >= f[i + 1])
				{
					co += f[i] + f[i + 1];
					f[i + 1] = 0;
				}
				else
				{
					co += f[i + 1] - f[i];
					f[i + 1] = 0;
				}
			}
		}

		cout << co << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int serch(char str) {
	if (str == 'I')
		return 1;
	else if (str == 'V')
		return 5;
	else if (str == 'X')
		return 10;
	else if (str == 'L')
		return 50;
	else if (str == 'C')
		return 100;
	else if (str == 'D')
		return 500;
	else
		return 1000;
}
int main() {
	int fron = 0, bac = 0, sum = 0;
	string str;
	while (cin >> str) {
		for (int i = 0; str[i] != '\0'; i++) {
			bac = serch(str[i]);
			if (fron >= bac)
				sum += bac;
			else
				sum += bac - fron * 2;
			fron = bac;
			bac = 0;
		}
		cout << sum << endl;
		sum = 0;
		fron = 0;
	}
}