//罗马数字转整数
import java.util.HashMap;
import java.util.*;
public class RomanToInteger {
public static int romanToInt(String s){
HashMap<Character,Integer> map=new HashMap<>();
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);
HashMap<Character,List<Character>> special=new HashMap<>();
special.put('I',Arrays.asList('V','X'));
special.put('X',Arrays.asList('L','C'));
special.put('C',Arrays.asList('D','M'));
int n=0;
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(i<s.length()-1){
char next=s.charAt(i+1);
if(special.containsKey(c)){
if(special.get(c).contains(next)){
int nextInt=map.get(next);
int curInt=map.get(c);
n+=(nextInt-curInt);
i++;
continue;
}
}
}
int curInt=map.get(c);
n+=map.get(c);
}
return n;
}
public static void main(String[] args) {
System.out.println(romanToInt("II"));
System.out.println(romanToInt("LVIII"));
System.out.println(romanToInt("IV"));
}
}