Topics excerpted from http://blog.csdn.net/dongyi91/article/details/38639915 wrote for 2 hours, level too dish out
The encoding principle of the method is: according to the existing code table, when the input pinyin and the number of characters after the output. For example:
There is the following code: XI _xi Wash _xi system _xi no _WU five _wu (between the characters and pinyin separated by an underscore), then when the input xi2, the result is the second character of the phonetic bit XI, namely "wash".
Input: Code
Hi _xi
Wash _xi
Department _xi
No _wu
Five _wu
Search
Xi2
End
Output: Wash
Note: When the query is not in the encoding table, the result is "Error"; when the input pinyin does not have a number, that is, "Xi", the output is: Pinyin is the first number of Xi's characters, but the maximum output of 5, that is, the output: HI; wash; department.
Research points: String segmentation, String conversion to integer type, Multimap use (key is pinyin, value is the corresponding character).
Package Huawei Machine test; import java.util.arraylist;import java.util.hashmap;import java.util.iterator;import Java.util.Scanner ;p Ublic class Main4 {public static void main (string[] args) {//TODO auto-generated method Stubscanner scn=new Scanner (Sys tem.in);//Read codehashmap<string,arraylist<string>> hash=new hashmap<string,arraylist<string> > (); String Cur=scn.next (); while (!cur.equals ("Search")) {String S[]=cur.split ("_"); System.out.println (s[0]+ "--" +s[1]); if (Hash.get (s[1]) ==null) {ArrayList arry=new arraylist<string> (); Arry.add (S[0]); Hash.put (S[1],arry); } else {hash.get (s[1]). Add (S[0]); } cur=scn.next (); }//Output hash value/* iterator<string> iter=hash.keyset (). Iterator (); while (Iter.hasnext ()) {System.out.println (Iter.next ()); } */ Cur=scn.next (); while (!cur.equals ("End")) {char Last=cur.charat (cur.length ()-1); System.out.println ("cur" + last); Separate the number of words if (last>= ' 0 ' &&last<= ' 9 ')///The last one is whether there are numbers {int i; For (I=0;i<cur.length (); i++) {char c=cur.charat (i); if (c> ' 0 ' &&c<= ' 9 ') break; } String key=cur.substring (0,i); int pos=integer.valueof (cur.substring (i)); if (Hash.get (key) ==null) System.out.println ("Error"); else {System.out.println (Hash.get (key). Get (POS)); }} else//not the number directly outputs the first 5, if any, {int count=0; For (String s:hash.get (cur)) {System.out.println (s); count++; if (count==5) break; }} cur=scn.next (); }}}
Huawei 2015 Simple Dictionary Input Method java