Algorithm Note _232: extract pinyin initials (JAVA)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description

In many software, you can quickly locate an entry by typing the first letter of the phonetic alphabet. For example, in the railway ticketing software, enter: "BJ" can be located to "Beijing." How do you implement this functionality in your own software? The crux of the problem is that each Chinese character must be able to calculate its phonetic initials.

In the GB2312 Chinese character coding way, 3,755 of the first-level Chinese characters are arranged in pinyin order. We can use this feature to find the first letter of pinyin for commonly used Chinese characters.

The GB2312 encoding scheme uses two bytes per kanji. The first byte is the area code, and the second byte is the offset number in the zone. In order to be compatible with existing ASCII encodings (in both Latin and Western), the area code and offset numbers begin with 0XA1.

We just need to find the pinyin a,b,c,... X, y, z each letter corresponds to the GB2312 encoded first character, you can locate all first-level Chinese characters Pinyin initials (regardless of the polyphone situation). The above information is given in the table below. Please use this table to write a program to find out the pinyin initials of commonly used Chinese characters.


a Ah, b0a1.
b Pattaya b0c5
C Rub b2c1
D-Lap B4ee
e moth B6ea
F FA b7a2
G-Karma B8C1
h ha b9fe
J hit BBF7
k Kerala BFA6
L C0AC
m mom C2e8
N Take c4c3
o Oh c5b6
p PA c5be
Q-Period C6da
R C8BB
s Isaac C8f6
T collapsed CBFA
W dig CDDA
X-CEF4
y-Pressure d1b9
Z-turns d4d1


"Input, output format requirements"

The user enters an integer n (n<100) First, indicating that there will be n lines of text next. Then enter the N-line string (no more than 50 Chinese characters per string).

The program outputs n lines, each line content is the first letter of the Chinese character of the corresponding line entered by the user.

No spaces between letters, all uppercase letters.

For example:
User input:
3
Everyone loves science.
Beijing Tiananmen Square
Software Contest

The program output:
Djakx
Bjtamgc
Rjds

2 Solutions
1 Importjava.io.UnsupportedEncodingException;2 ImportJava.util.Scanner;3 4  Public classMain {5      Public StaticString[] Arraya = {"Ah", "BAA", "rub", "lap", "moth", "FA", "Karma", "Ha",6"Hit", "Kerala", "garbage", "Mom", "take", "Oh", "Pa", "period", "but", "Satan", "collapse", "Dig", "ever",7"Pressure", "turns"}; 8      Public Static int[] Valuea =New int[23];9      Public Static Char[] Resulta = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' J ', ' K ',Ten' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' W ', ' X ', ' Y ', ' Z '}; One      A      Public intgetnum (String A) { -         byte[] A =NULL; -         Try { theA = A.getbytes ("GB2312"); -}Catch(unsupportedencodingexception e) { - e.printstacktrace (); -         } +         intB = ((a[0]<<8) &0xff00) + (A[1]&0xff); -         returnb; +     } A      at      Publicstring GetResult (String A) { -         intnum =Getnum (A); -         inti = 0; -          for(; I < 23;i++) -             if(Valuea[i] >num) -                  Break; inString r = "" + resulta[i-1]; -         returnR; to     } +      -      Public Static voidMain (string[] args) { theMain test =NewMain (); *          for(inti = 0;i < 23;i++) $Valuea[i] =Test.getnum (Arraya[i]);Panax NotoginsengScanner in =NewScanner (system.in); -         intn =in.nextint (); theString[] result =NewString[n]; +          for(inti = 0;i < n;i++) { AString A =In.next (); theStringBuffer s =NewStringBuffer (""); +              for(intj = 0;j < A.length (); j + +) { -String temp = "" +A.charat (j); $ s.append (Test.getresult (temp)); $             } -Result[i] =s.tostring (); -         } the          for(inti = 0;i < n;i++) - System.out.println (Result[i]);Wuyi     } the}

Operation Result:

4 Building a new socialist rural Europe give me a fulcrum I can pry the earth how JSSHZYXNCOZGWYGZDWKYQDDQZ

Algorithm Note _232: extract pinyin initials (JAVA)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.