首页 > 专题系列 > Java解POJ > POJ 2608 Soundex [解题报告] Java
2013
11-11

POJ 2608 Soundex [解题报告] Java

Soundex

问题描述 :

Soundex coding groups together words that appear to sound alike based on their spelling. For example, “can” and “khawn”, “con” and “gone” would be equivalent under Soundex coding.

Soundex coding involves translating each word into a series of digits in which each digit represents a letter:
      1 represents B, F, P, or V

2 represents C, G, J, K, Q, S, X, or Z
3 represents D or T
4 represents L
5 represents M or N
6 represents R

The letters A, E, I, O, U, H, W, and Y are not represented in Soundex coding, and repeated letters with the same code digit are represented by a single instance of that digit. Words with the same Soundex coding are considered equivalent.

输入:

Each line of input contains a single word, all upper case, less than 20 letters long.

输出:

For each line of input, produce a line of output giving the Soundex code.

样例输入:

KHAWN
PFISTER
BOBBY

样例输出:

25
1236
11

解题代码:

//* @author 洪晓鹏<[email protected]>
import java.util.Scanner;

public class Main {
	private static final int Q = 0;
	public static int[][] array = { { 'B', 'F', 'P', 'V' },
		{ 'C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z' }, { 'D', 'T' }, { 'L' },
		{ 'M', 'N' }, { 'R' }, { 'A', 'E', 'I', 'O', 'U', 'H', 'W', 'Y' } };

   public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    while (in.hasNext()) {
	String s = in.nextLine();
	String result = "";

	for (int i = 0; i < s.length(); i++) {
               boolean find = false;
		for (int j = 0; j < 7; j++) {
			int len = array[j].length;
			for (int k = 0; k < array[j].length; k++) {
				if (array[j][k] == s.charAt(i)) {
					find = true;
					result += (j + 1);
					break;
				}
			}
			if (find == true)
				break;
		}
	}
	String res = "";
	if (result.charAt(0) < '7')
		res += result.charAt(0);
	for (int i = 1; i < result.length(); i++) {
		if (result.charAt(i) < '7'
			&& result.charAt(i) != result.charAt(i - 1))
			res += result.charAt(i);
	}
	System.out.println(res);
   }
  }
}

  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。

  3. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.