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>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#include<complex>
using namespace std;

typedef complex<double> P;

#define X real()
#define Y imag()


int main(){
    int mod=1000000007;
    map<char, int> d;
    d['I']=1;
    d['V']=5;
    d['X']=10;
    d['L']=50;
    d['C']=100;
    d['D']=500;
    d['M']=1000;
    char c[60];
    int len,t,value;
    while (fgets(c,60,stdin)!=NULL)  {
        len=strlen(c)-1;
        t=0,value=1000;
        for (int i=0;i<len;++i) {
            if (value>=d[c[i]]) {
                value=d[c[i]];
                t+=value;
            } else {
                t=t-value*2;
                value=d[c[i]];
                t+=value;
            }
        }
        printf("%d\n",t);
    }
    return 0;
}

C++: #include<cstdio>

using namespace std;

int main(void){
	char in[100];
	int i,size,data,com,flag=0,ans=0;

	while(scanf("%s",in)!=EOF){
		for(i=0;i<100;i++){
			if(in[i]=='\0')	break;
		}
		size=i;

		for(i=0;i<size;i++){
			if(in[i]=='I')	data=1;
			if(in[i]=='V')	data=5;
			if(in[i]=='X')	data=10;
			if(in[i]=='L')	data=50;
			if(in[i]=='C')	data=100;
			if(in[i]=='D')	data=500;
			if(in[i]=='M')	data=1000;
			
			if(flag==1 && com>data)	flag=2;
			if(flag==1 && com<data)	flag=3;
			if(flag==1 && com==data){
				ans+=data;
				com=data;
			}

			if(flag==2 && com>=data){
				ans+=data;
				com=data;
			}
			if(flag==2 && com<data)	flag=0;
			
			if(flag==3 && com<=data){
				ans-=data;
				com=data;
			}
			if(flag==3 && com>data)	flag=0;

			if(flag==0){
				ans+=data;
				com=data;
				flag=1;
			}
		}
		if(ans<0)	ans*=(-1);
		printf("%d\n",ans);
		ans=0;
		flag=0;
	}

	return 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]);
	if(i+1==count) ans += a[i];
	i++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: //0039
//include-------------------------------------
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<stack>

using namespace std;

//typedef--------------------------------------
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define MP make_pair

//repetition-----------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

//constant-------------------------------------
const double PI = acos(-1.0);

//cout,cin-------------------------------------
#define COUT(x) cout<<(x)<<endl
#define CIN(x) cin>>(x)

int f(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 if (c == 'M') return 1000;
}

int main(){
	int ans = 0;
	string st;
	while (CIN(st)){
		 int now = f(st[0]);
		 if (st.size() == 1) ans = now;
		 ans = now;
		 REP(i, st.size() - 1){
			 now >= f(st[i + 1]) ? ans += f(st[i + 1]) : ans += f(st[i + 1]) - now - now;
			 now = f(st[i + 1]);
		 }
		 COUT(ans);
	}



	return 0;

}

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

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

C++: #include <bits/stdc++.h>
using namespace std;
char m[7]={'I','V','X','L','C','D','M'};
int n[7]={1,5,10,50,100,500,1000};
int d[110];
int main(){
	string s;
	while(cin>>s){
		for(int i=0;i<s.size();i++){
			for(int j=0;j<7;j++){
				if(m[j]==s[i])d[i]=n[j];
			}
		}
		int ans=0;
		for(int i=0;i<s.size();i++){
			bool t=false;
			for(int j=i+1;j<s.size();j++){
				if(d[j]>d[i])t=true;
			}
			if(t)ans-=d[i];
			else ans+=d[i];
		}
		cout<<ans<<endl;
	}
	getchar();
	getchar();
	return 0;
}

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

int main() {
  string str;
  int size,count = 1,ans = 0,xx[100] = {0};
  char x[100];
  while(cin >> str) {
    size = str.size();
    for(int i=0;i<size;i++) {
      x[i] = *str.substr(i,1).c_str();
      if(x[i] == 'I') {
	xx[i] = 1;
      }
      else if(x[i] == 'V') {
	xx[i] = 5;
      }
      else if(x[i] == 'X') {
	xx[i] = 10;
      }
      else if(x[i] == 'L') {
	xx[i] = 50;
      }
      else if(x[i] == 'C') {
	xx[i] = 100;
      }
      else if(x[i] == 'D') {
	xx[i] = 500;
      }
      else if(x[i] == 'M') {
	xx[i] = 1000;
      }
    }

    for(int i=0;i<size-1;i++) {
      if(xx[i] == xx[i+1]) {
	count++;
	if(i == size-2) {
	  ans += xx[i+1] * count;
	  count = 1;
	}
      }
      else {
	if(xx[i] < xx[i+1]) {
	  ans -= xx[i] * count;
	  count = 1;
	}
	else if(xx[i] > xx[i+1]) {
	  ans += xx[i] * count;
	  count = 1;
	}
	if(i == size-2) {
	  ans += xx[i+1] * count;
	  count = 1;
	} 
      }
    }
    cout << ans << endl;
    ans = 0;
  }
}

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

int n;
char buf[1000];

int main() {
  while (1) {
    if (scanf("%s", buf) != 1) break;

    n = strlen(buf);
    int total = 0;
    int cur_num = 0;
    int prev_num = 0;
    for (int i=0; i<n; i++) {
      switch (buf[i]) {
      case 'I':
        cur_num = 1;
        break;
      case 'V':
        cur_num = 5;
        break;
      case 'X':
        cur_num = 10;
        break;
      case 'L':
        cur_num = 50;
        break;
      case 'C':
        cur_num = 100;
        break;
      case 'D':
        cur_num = 500;
        break;
      case 'M':
        cur_num = 1000;
      }

      if (prev_num >= cur_num) total += cur_num;
      else total += cur_num - prev_num*2;
      prev_num = cur_num;
    }

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

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main(){

	string str;
	int table[200];

	table[0]=0;
	table['I']=1;
	table['V']=5;
	table['X']=10;
	table['L']=50;
	table['C']=100;
	table['D']=500;
	table['M']=1000;

	while(cin>>str){
		reverse(str.begin(),str.end());
		int prv=0;
		int sum=0;
		for(int i = 0; i < str.size(); i++){
			if(prv>table[str[i]]){
				sum-=table[str[i]];
			}
			else{
				sum+=table[str[i]];
			}
			prv=table[str[i]];
		}
		cout<<sum<<endl;
	}
	return 0;
}