trial-name of husband and wife, name of husband and wife

Source: Internet
Author: User

Trial-name of husband and wife, name of husband and wife

Question: Husband and Wife


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"

Running time limit: Unlimited
Memory limit: Unlimited

Input: enter a man's name, string
Output: name of the lady with the most "Husband and Wife"


Basic Ideas:

Find the letters in the men's and women's names, such as wang yun. The letters are a, g, n, y, u, w. then match the number of duplicate letters in the two names. If both of them contain a letter, add 1 to the number of duplicates.



# Include <iostream> # include <string> using namespace std; // 10 names, 26 characters char nameArray [10] [26] = {"wang fei ", "zhang man yu", "zhang zhi yi", "li", "li xiao man", "li yu cun", "yang ni", "xiao tong ", "li lei", "zhang san"}; // specifies the number of times that the letters in a and B are repeated. int findCount (bool a [], bool B []) {int count = 0; for (int I = 0; I <26; I ++) {if (a [I] & B [I]) count ++ ;} return count;} int main () {char name [100]; gets (name); bool flag_0 [10] [26]; // The letter recorded in each lady's name, which is 1 and not 0 memset (flag_0, 0, sizeof (flag_0); int I, j; for (I = 0; I <10; I ++) {for (j = 0; nameArray [I] [j]! = '\ 0'; j ++) {if (nameArray [I] [j] <= 'Z' & nameArray [I] [j]> = 'A ') {flag_0 [I] [nameArray [I] [j]-'a'] = 1 ;}} bool flag_1 [26]; // The letters recorded in each man's name, which are 1 and not 0 memset (flag_1, 0, sizeof (flag_1); for (I = 0; name [I]! = '\ 0'; I ++) {if (name [I] <= 'Z' & name [I]> = 'A ') {flag_1 [name [I]-'a'] = 1 ;}} int count [10]; // record the repeated letters in each lady's name and the men's name. int max = 0; for (I = 0; I <10; I ++) {count [I] = findCount (flag_1, flag_0 [I]); if (count [I]> max) max = count [I]; // maximum number of output matching} for (I = 0; I <10; I ++) {if (count [I] = max) {cout <nameArray [I] <endl; // output the first matched lady name break;} return 0 ;}



Test results may be incomplete. Check for missing items:





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.