/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

import java.util.*;

public class Solution 
{
    
    public static void main (String []args)
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("DECIMAL to ROMAN NUMERAL CONVERSION. REFRAIN FROM SETTING ABOVE 3999 DUE TO CHANGE IN CONVENTION");
        for (int i=1; i<4000; i++)
        {
            System.out.println("Decimal: " + i + " => " + decimalToRoman(i));
        }
    }
    
    
    public static String decimalToRoman(int num) 
    {
        /*
        String [][] numeralsCorrection = new String[][]{   
                                                {"IIII","IV"},{"VIIII","IX"},{"XIIII","XIV"}, {"LIIII","LIV"},{"CIIII","CIV"},{"DIIII","DIV"},{"MIIII","MIV"},           
                                                {"XXXX","XL"},{"LXXXX","XC"},{"XIIII","XIV"}, {"LIIII","LIV"},{"CIIII","CIV"},{"DIIII","DIV"},{"MIIII","MIV"},            
                                                        };
        */
        
        String beyondIncorrectNumeral="";
        String temp="";
        String conversion="";
        int currentNumeral=0;
        

        //M=1000, C=100, D=500, L=50, X=10,  V=5,  I=1


        //these have to be descending order

        //return "test";

        //I will first attempt this from recursive perspective..
        //using a class level variable

        //This should much easier than conversion numeral => decimal number

        //we only need to worry about the valid subtractive notations (as a pair)
        
        //do not need to worry about adjacent subtractive notation being in the same range

        //not relevant
        //i.e  IX XC
             //range (1-10)  (range 10-100)

        // these are the valid combinations....

        //String IV =  "IV";
        //String IX =  "IX";
        //String XL =  "XL";
        //String XC =  "XC";
        //String CD=   "CD"; 
        //String CM=   "CM";

        //Also need to consider this rule
        //The xcletters V, L, and D are not repeated.


        //I will work this example using the following two numbers
        //27  and also 19
        //both will bring their own challenges

       
        //main structure of code
        if (num==0)
        {
            return conversion;
        }
                  
        else
        {
            //M=1000, C=100, D=500, L=50, X=10,  V=5,  I=1

            //only way to tackle this challenge is using an if else
            //and check if larger than numbers
            //1000, 500, 100, 50, 10, 5 , 1
            //Map will not work!!!
            //Switch will not work!!!
            //So similar to numeral => decimal, there will be no collections!!!!!

            //do
            //{

                do
                {
                    if (num >= 1000) 
                    {
                        currentNumeral++;
                        conversion = conversion + "M";
                        num=num-1000;
                        //System.out.println("num is greater than: " + 1000);
                    } 
                    else if (num>=500) 
                    {
                        currentNumeral++;
                        conversion = conversion + "D";
                        num=num-500;
                        //System.out.println("num is greater than: " + 500);
                    }
                    else if (num>=100) 
                    {
                        currentNumeral++;
                        conversion = conversion + "C";
                        num=num-100;
                        //System.out.println("num is greater than: " + 100);
                    }
                    else if (num>=50) 
                    {
                        currentNumeral++;
                        conversion = conversion + "L";
                        num = num - 50;
                        //System.out.println("num is greater than: " + 50);
                    }
                    else if (num>=10) 
                    {
                        currentNumeral++;
                        conversion = conversion + "X";
                        num = num -10;
                        //System.out.println("num is greater than: " + 10);
                    }
                    else if (num>=5) 
                    {
                        currentNumeral++;
                        conversion = conversion + "V";
                        num = num - 5;
                        //System.out.println("num is greater than: " + 5);
                    }
                    else if (num>=1)
                    {
                        currentNumeral++;
                        conversion = conversion + "I";
                        num = num - 1;
                        //System.out.println("num is greater than: " + 1);
                    }
                    else
                    {
                        System.out.println("Unknown error, code will terminate");
                        System.exit(0);
                    }
                }while (num>0);
        
                System.out.println(conversion);
        
                if (conversion.indexOf("MCCCC")!=-1)
                {
                    //if more characters beyond last C of MCCCC
                    if ((conversion.indexOf("MCCCC")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("MCCCC")+5);
                    }
                     //if more characters in front
                        if (conversion.substring(0,conversion.indexOf("MCCCC")).length()>0)
                        {
                            temp = conversion.substring(0,conversion.indexOf("MCCCC"));
                            //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                            conversion = temp + "MCD" + beyondIncorrectNumeral;
                            //System.out.println("original => conversion: " + temp + " => " + conversion);
                        }
                        else
                        {
                            //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                            conversion = "MCD" + beyondIncorrectNumeral;
                            //System.out.println("original => conversion: " + temp + " => " + conversion);
                        }
                        beyondIncorrectNumeral="";
                }
        
        
                if (conversion.indexOf("DCCCC")!=-1)
                {
                        //if more characters beyond last C of DCCCC
                        if ((conversion.indexOf("DCCCC")+4)!=conversion.length()-1)
                        {
                            beyondIncorrectNumeral = conversion.substring(conversion.indexOf("DCCCC")+5);
                        }
                        //if more characters in front
                        if (conversion.substring(0,conversion.indexOf("DCCCC")).length()>0)
                        {
                            temp = conversion.substring(0,conversion.indexOf("DCCCC"));
                            //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                            conversion = temp + "CM" + beyondIncorrectNumeral;
                            //System.out.println("original => conversion: " + temp + " => " + conversion);
                        }
                        else
                        {
                            //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                            conversion = "CM" + beyondIncorrectNumeral;
                            //System.out.println("original => conversion: " + temp + " => " + conversion);
                        }
                        
                        beyondIncorrectNumeral="";
                }
                    
                    
                if (conversion.indexOf("CCCC")!=-1)
                {
                        if(conversion.length()>4)
                        {
                            conversion = "CD" + conversion.substring(conversion.indexOf("CCCC")+4);
                        }
                        else
                        {
                            conversion = "CD";
                        }
                }
                    
                //System.out.println("------------------------------");
            
                if (conversion.indexOf("MXXXX")!=-1)
                {
                    //if more characters beyond last X of MXXXX
                    if ((conversion.indexOf("MXXXX")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("MXXXX")+5);
                
                    }
                    //if more characters in front
                    if (conversion.substring(0,conversion.indexOf("MXXXX")).length()>0)
                    {
                        temp = conversion.substring(0,conversion.indexOf("MXXXX"));
                        //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                        conversion = temp + "MXL" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    
                    else
                    {
                        //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                        conversion = "MXL" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
            
                    beyondIncorrectNumeral="";
                }
                    
                    
                if (conversion.indexOf("DXXXX")!=-1)
                {
                    //if more characters beyond last X of DXXXX
                    if ((conversion.indexOf("DXXXX")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("DXXXX")+5);
                
                    }
                    //if more characters in front
                    if (conversion.substring(0,conversion.indexOf("DXXXX")).length()>0)
                    {
                        temp = conversion.substring(0,conversion.indexOf("DXXXX"));
                        //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                        conversion = temp + "DXL" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    else
                    {
                        //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                        conversion = "DXL" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    beyondIncorrectNumeral="";
                }
            
                //********************************8
            
                if (conversion.indexOf("CXXXX")!=-1)
                {
                    //if more characters beyond last X of CXXXX
                    if ((conversion.indexOf("CXXXX")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("CXXXX")+5);
                    }
                    //if more characters in front
                    if (conversion.substring(0,conversion.indexOf("CXXXX")).length()>0)
                    {
                        temp = conversion.substring(0,conversion.indexOf("CXXXX"));
                            //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                        conversion = temp + "CXL" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    else
                    {
                        //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                        conversion = "CXL" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    beyondIncorrectNumeral="";
                }    
            
                if (conversion.indexOf("LXXXX")!=-1)
                {
                    //if more characters beyond last X of LXXXX
                    if ((conversion.indexOf("LXXXX")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("LXXXX")+5);
                    }
                    //if more characters in front
                    if (conversion.substring(0,conversion.indexOf("LXXXX")).length()>0)
                    {
                        temp = conversion.substring(0,conversion.indexOf("LXXXX"));
                            //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                        conversion = temp + "XC" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    else
                    {
                        //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                        conversion = "XC" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    beyondIncorrectNumeral="";
                }
        
                if (conversion.indexOf("XXXX")!=-1)
                {
                    if(conversion.length()>4)
                    {
                        conversion = "XL" + conversion.substring(conversion.indexOf("XXXX")+4);
                        //System.out.println("Processes if");
                    }
                    else
                    {
                        //System.out.println("Processes else");
                        conversion = "XL";
                    }
                }
                    //System.out.println("------------------------------");
            
                if (conversion.indexOf("MIIII")!=-1)
                {
                    //if more characters beyond last I of MIIII
                    if ((conversion.indexOf("MIIII")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("MIIII")+5);
                    }
                    //if more characters in front
                    if (conversion.substring(0,conversion.indexOf("MIIII")).length()>0)
                    {
                        temp = conversion.substring(0,conversion.indexOf("MIIII"));
                        //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                        conversion = temp + "MIV" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    else
                    {
                        //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                        conversion = "MIV" + beyondIncorrectNumeral;
                            //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    beyondIncorrectNumeral="";
                }
                    
                if (conversion.indexOf("DIIII")!=-1)
                {
                    //if more characters beyond last I of DIIII
                    if ((conversion.indexOf("DIIII")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("DIIII")+5);
                    }
                    
                    //if more characters in front
                    if (conversion.substring(0,conversion.indexOf("DIIII")).length()>0)
                    {
                        temp = conversion.substring(0,conversion.indexOf("DIIII"));
                        //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                        conversion = temp + "DIV" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    else
                    {
                        //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                        conversion = "DIV" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                        beyondIncorrectNumeral="";
                }
        
                if (conversion.indexOf("CIIII")!=-1)
                {
                    //if more characters beyond last I of CIIII
                    if ((conversion.indexOf("CIIII")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("CIIII")+5);
                    }
                    //if more characters in front
                    if (conversion.substring(0,conversion.indexOf("CIIII")).length()>0)
                    {
                        temp = conversion.substring(0,conversion.indexOf("CIIII"));
                        //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                        conversion = temp + "CIV" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    else
                    {
                        //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                        conversion = "CIV" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                        beyondIncorrectNumeral="";
                }
        
                if (conversion.indexOf("LIIII")!=-1)
                {
                    //if more characters beyond last I of LIIII
                    if ((conversion.indexOf("LIIII")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("LIIII")+5);
                    }
                     //if more characters in front
                    if (conversion.substring(0,conversion.indexOf("LIIII")).length()>0)
                    {
                        temp = conversion.substring(0,conversion.indexOf("LIIII"));
                        //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                        conversion = temp + "LIV" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    else
                    {
                        //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                        conversion = "LIV" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    beyondIncorrectNumeral="";
                }
                if (conversion.indexOf("XIIII")!=-1)
                {
                    //if more characters beyond last I of XIIII
                    if ((conversion.indexOf("XIIII")+4)!=conversion.length()-1)
                    {
                        beyondIncorrectNumeral = conversion.substring(conversion.indexOf("XIIII")+5);
                    }
                    //if more characters in front
                    if (conversion.substring(0,conversion.indexOf("XIIII")).length()>0)
                    {
                        temp = conversion.substring(0,conversion.indexOf("XIIII"));
                        //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                        conversion = temp + "XIV" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    else
                    {
                        //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                        conversion = "XIV" + beyondIncorrectNumeral;
                        //System.out.println("original => conversion: " + temp + " => " + conversion);
                    }
                    beyondIncorrectNumeral="";
                }
        
                if (conversion.indexOf("VIIII")!=-1)
                {
                    //System.out.println("THIS PATTERN");
                    //System.out.println("Last index Conversion: " + (conversion.length()-1));
                    //System.out.println(conversion.indexOf("VIIII")+4);
                    //System.out.println("Length string before the match: " + conversion.substring(0,conversion.indexOf("VIIII")).length());
            
                    if (conversion.indexOf("VIIII")!=-1)
                    {
                        //if more characters beyond last I of VIIII
                        if ((conversion.indexOf("VIIII")+4)!=conversion.length()-1)
                        {
                            beyondIncorrectNumeral = conversion.substring(conversion.indexOf("VIIII")+5);
                        }
                        //if more characters in front
                        if (conversion.substring(0,conversion.indexOf("VIIII")).length()>0)
                        {
                            temp = conversion.substring(0,conversion.indexOf("VIIII"));
                            //System.out.println("SHOULD BE HERE IF STATEMENT");
                
                            conversion = temp + "IX" + beyondIncorrectNumeral;
                            //System.out.println("original => conversion: " + temp + " => " + conversion);
                        }
                        else
                        {
                            //System.out.println("SHOULD BE HERE ELSE STATEMENT");
                            conversion = "IX" + beyondIncorrectNumeral;
                            //System.out.println("original => conversion: " + temp + " => " + conversion);
                        }
                        beyondIncorrectNumeral="";
                    }
                }
                    
                if (conversion.indexOf("IIII")!=-1)
                {
                    //we can not use this logic here, since it will evalulate as 0.
                    //And performing substring as this as delimiter will match the start
                    //and give StringIndexOutOfBoundsException
                    //if (conversion.substring(0,conversion.indexOf("IIIII")).length()>0)
            
                    if(conversion.length()>4)
                    {
                        System.out.println("IF STATEMENT");
                        conversion = "IV" + conversion.substring(conversion.indexOf("IIII")+4);
                    }
                    else
                    {
                        System.out.println("ELSE STATEMENT");
                        conversion = "IV";
                    }
                }
        
                    //System.out.println("------------------------------");
        
                    //we need to address each scenario from the most left hand side first
                    //I do not think it will matter either way...
                    /*
        
                    if m before CCCC =>  MCD                                       (MCD)
                    if   d before CCCC  => CM                                      (CM)
                    if nothing before CCCC => CD                                   (CD)
        
                    if M before XXXX => MXL                                         (MXL)
                    if D before XXXX => DXL                                         (DXL)
                    if C before XXXX => CXL                                         (CXL)
                    if L before XXXX  => XC                                         (XC)
        
                    if nothing before XXXX => XL                                    (XL)
            
        
                    if D before IIII => D followed by IV                          (DIV)
                    if L before IIII => L followed by IV                          (LIV)
                    if X before IIII => X followed by IV                          (XIV)
                    if  V before IIII  => I followed by next highest letter to V  (IX)
                    if nothing before IIII  =>                                    (IV)
                    */
            }  //end of else    

            return conversion;  
    }
}