题目:
屏幕上给出m个单词,10s时间让小易记忆。
小易给出记住的单词数n个。小易的分数等于完全答对的单词的长度的平方和。
输入n和m,小易默写出的单词,屏幕给出的单词,输出小易的得分。例如:
输入:
2 3
bad good
hello bad wood
输出
9
思路分析:使用set存储小易默写的单词(需要去重,有可能小易给出的答案中有两个相同的单词,所以用set)和屏幕给出的单词。然后遍历小易给出的单词,并查看其是否在屏幕给出的单词中。
#include <iostream>
#include <stdio.h>
#include <set>
#include <cstring>
#include <string>
using namespace std;
int main() {
int result = 0;
int n, m;
set<string> res;//小易猜到的答案
set<string> qes;//系统提供的单词
cin >> n >> m;
for(int i = 0; i < n; i ++){
string x;
cin >> x;
res.insert(x);
}
for(int j = 0; j < m; j ++){
string x;
cin >> x;
qes.insert(x);
}
set<string>::iterator iter = res.begin();
while(iter != res.end()){
if(qes.find(*iter) != qes.end()){
string a = *iter;
result +=( a.size() )*( a.size());
}
iter ++;
}
cout << result << endl;
return 0;
}