Huawei hands-on exercises-Name: Husband and Wife

Source: Internet
Author: User

Question:

In China, most people use the word "Husband and Wife ". The so-called "Husband and Wife" means that two people seem to be perfect, and there are some similarity in appearance, body, and other aspects. This question is another way to find the most "Husband and Wife" from the perspective of human name and the number of repeated letters.

The name of a group of ladies is prefixed in the question. Enter the name of the man in pinyin (there can be spaces in the middle of the pinyin text, and all the letters are in lower case), traverse all the names according to the preconfigured name in pinyin, and output the name of the lady with the most repeated letters.

Rule 1: If multiple women with the most repeated letters are the same, the first matched lady will be the most "Husband and Wife" candidate.

Rule 2: the same letters in a person's name are processed once.

For example, the number of repeated characters between Li Ling and Li Lei is 2, not 4.

Preset ladies list (must be ensured in sequence ):
"Wang Fei ",
"Zhang Man Yu ",
"Zhang Zhi Yi ",
"Li ",
"Li Xiao man ",
"Li Yu Cun ",
"Yang Ni ",
"Xiao Tong ",
"Li Lei ",
"Zhang San"

Run Time Limit: unlimited memory limit: Unlimited

Input: enter a man's name, string

Output: name of the lady with the most "Husband and Wife"

Analysis: From this question, we can also see the heavy shadow, so first final the set, and then judge the number of characters in turn cyclically, so this is also a very simple question, let's talk about the code;

The Code is as follows:

Package com. wenj. test;

Import java. util. iterator;
Import java. util. linkedhashset;
Import java. util. Set;

Public class testselectthecouple {

Public static void main (string ARGs []) {
String strin = "Li Ling ";
Testselectthecouple Ts = new testselectthecouple ();
System. Out. println (TS. selectthecouple (strin ));
}

Public String selectthecouple (string strin ){
String strtemp = strin. replaceall ("","");
String [] Warr = {"Wang Fei", "Zhang Man Yu", "Zhang Zhi Yi ",
"Li", "Li Xiao man", "Li Yu Cun", "Yang Ni", "Xiao Tong ",
"Li Lei", "Zhang San "};
Int [] P = new int [Warr. Length]; // an array containing the same number of characters

Char [] CA = strtemp. tochararray ();
Set <character> MS = new linkedhashset <character> ();
For (INT I = 0; I <ca. length; I ++ ){
Ms. Add (Ca [I]);
}

Int COUNT = 0;
For (INT I = 0; I <Warr. length; I ++) {// determine the number of characters that each female and the input male have, and store the results in P
For (iterator <character> it = Ms. iterator (); it. hasnext ();){
Char temp = it. Next ();
If (WARR [I]. Contains (temp + "")){
Count ++;
}
}
P [I] = count;
Count = 0;
}

Int max = P [0];
Int Pos = 0;
For (INT I = 1; I <p. length; I ++) {// find
If (P [I]> MAX ){
Max = P [I];
Pos = I;
}
}

Return Warr [POS]; // output the best female pig
}
}

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.