华为机试热身之夫妻相

描述

在中国,形容夫妻恩爱的词汇中,大家用的比较多的就是“夫妻相”。所谓“夫妻相”,就是两个人看上去比较般配,长相、身材等某些方面有一定的相似度。
本题则另辟蹊径,从人的姓名维度,以字母重复个数来寻找最具“夫妻相”的人。
题目中预先给定一组女士的姓名拼音。输入男士的姓名拼音(拼音中间可以有空格,字母全部小写),依预先给定姓名拼音的先后遍历所有姓名,输出字母重复数最多的女士姓名。

规则1:如果字母重复数最多的女士有多位相同,则以最先匹配的女士做为最具“夫妻相”的人选。
规则2:人名中的相同字母,按重复一次处理。例如:li ling 与li lei 重复的字符个数为2,而不是4。
预置女士名单(先后循序必须保证):
“wang fei”,
“zhang man yu”,
“zhang zhi yi”,
“li li”,
“li xiao man”,
“li yu cun”,
“yang ni”,
“xiao tong”,
“li lei”,
“zhang san”
运行时间限制: 无限制
内存限制: 无限制
输入: 输入一个男士姓名,字符串
输出: 输出最具“夫妻相”的女士姓名

不知道写的对不对

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include<map>
#include<vector>
#include<iostream>
#include<string>
using namespace std;
map<char, bool> mapp[10];
string str[] = { "wang fei", "zhang man yu", "zhang zhi yi", "li li",
"li xiao man", "li yu cun", "yang ni", "xiao tong", "li lei", "zhang san" };
void init()
{

for (int i = 0; i < 10; i++)
{
for (int j = 0; j < str[i].length(); j++)
{
if (str[i][j]!=' '&&mapp[i].find(str[i][j])==mapp[i].end())
{
mapp[i][str[i][j]] = true;
}
}
}
}

int main()
{

init();
char str2[100];
while (gets(str2))
{
string s1 = str2;
int max = 0;
int index = -1;
for (int j = 0; j < 10; j++)
{
int count = 0;
for (int i = 0; i < s1.length(); i++)
{
if (s1[i] != ' '&&mapp[j].find(s1[i]) != mapp[j].end())
{
count++;
}
}
if (count>max)
{
max = count;
index = j;
}
}
cout <<str[index]<< endl;
}
return 0;
}