Pages

Sunday 18 November 2012

UVA - 11787 - Numeral Hieroglyphs

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        int cases=Integer.parseInt(br.readLine());
        for(int i=0;i<cases;i++){
            String m=br.readLine();
            int[]arr=new int[7];
            boolean inc=false;
            boolean dec=false;
            boolean sucess=true;
            int last=indFind(m.charAt(0));
            for(int j=0;j<m.length();j++){
                int temp=indFind(m.charAt(j));
                if(temp>-1){
                    arr[temp]++;
                    if(arr[temp]>9){
                        sucess=false;
                        break;
                    }
                }
                if(temp>last){
                    inc=true;
                }else if(temp<last){
                   dec=true;
                }
                if(inc && dec){
                    sucess=false;
                    break;
                }
                last=temp;
            }
            if(sucess){
                boolean flag=false;
                for(int j=6;j>-1;j--){
                    if(arr[j]>0 ||flag){
                        sb.append(arr[j]);
                        flag=true;
                    }
                }
                sb.append("\n");
            }else{
                sb.append("error\n");
            }
        }
        System.out.print(sb);
    }
  
    static int indFind(char x){
        if(x=='B')
            return 0;
        if(x=='U')
            return 1;
        if(x=='S')
            return 2;
        if(x=='P')
            return 3;
        if(x=='F')
            return 4;
        if(x=='T')
            return 5;
        if(x=='M')
            return 6;
        else
            return -1;
    }
}

No comments:

Post a Comment