Pages

Saturday 17 November 2012

UVA - 10260 - Soundex

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("");
        String m = "";
        while ((m = br.readLine()) != null) {
            int last=-1;
            for(int i=0;i<m.length();i++){
                    int temp=matching(m.charAt(i));
                    if(temp>0 &&temp!=last) {
                        sb.append(temp);
                    }
                    last=temp;
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }

    static int matching(char x) {
        if (x == 'B' || x == 'F' || x == 'P' || x == 'V') {
            return 1;
        }
        if (x == 'C' || x == 'G' || x == 'J' || x == 'K'
                || x == 'Q' || x == 'S' || x == 'X' || x == 'Z') {
            return 2;
        }
        if (x == 'D' || x == 'T') {
            return 3;
        }
        if (x == 'L') {
            return 4;
        }
        if (x == 'M' || x == 'N') {
            return 5;
        }
        if (x == 'R') {
            return 6;
        }
        return 0;
    }
}

No comments:

Post a Comment