Soundex

From Progteam

Revision as of 00:56, 23 May 2008 by Mlc413 (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Soundex
Problem Number 2608
Sorter: mlc413
Source: Unknown
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=2608




Soundex is problem number 2608 on the Peking University ACM site.

Checkmark.jpg This problem has been solved by Mlc413.


import java.util.*;

public class Main{

    public static Scanner in;
 
    public static void main(String[] args){
        in=new Scanner(System.in);

        doStuff();
    }

    public static void doStuff(){
        while(in.hasNext()){
            solve(in.next());
        }
    }

    public static void solve(String word){
	int len = word.length();
	String newS = "";
	

	for(int i = 0; i < len; i++){
	    int num = 0;
	    char ch = word.charAt(i);
	    num = getNum(ch);
	    
	    
	    if (num > 0){
		if(i == 0){
		    newS+=""+num;
		    
		}
		else{
		    if(getNum(word.charAt(i))!=getNum(word.charAt(i-1))){
		    newS+=""+num;
		    
		    }
		    
		}
	    }
	    
	}
	
	System.out.println(newS);
    }

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

Personal tools