输入数据包括三行:
第一行为两个整数n(1 ≤ n ≤ 50)和m(1 ≤ m ≤ 50)。以空格分隔
第二行为n个字符串,表示小易能记住的单词,以空格分隔,每个单词的长度小于等于50。
第三行为m个字符串,系统提供的单词,以空格分隔,每个单词的长度小于等于50。
输出一个整数表示小易能获得的分数
3 4 apple orange strawberry strawberry orange grapefruit watermelon
136
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); Set<String> set=new HashSet<String>(); for(int i=0;i<n;i++){ set.add(sc.next()); } int score=0; for(int i=0;i<m;i++){ String tmp=sc.next(); if(set.contains(tmp)){ score+=(int)Math.pow(tmp.length(), 2); } } System.out.println(score); } }
/* 先将两行输入分别读入line1,line2; 把line2即系统提供的单词解析出来,存入set<string> s2; 把line1即用户默写的单词解析出来,看是否在s2中存在,若存在,则存入set<string> s1; 由于set具有多个同一元素只存一个的特性,因此遍历s1,取出元素的长度求平方再相加即可; */ #include<iostream> #include<string> #include <set> using namespace std; /*t10*/ int main(void) { int n, m; set<string> s1, s2; cin >> n >> m; cin.get(); string line1,line2; getline(cin,line1); getline(cin, line2); int pos1 = 0,pos2; for (int i = 0; i < m; ++i) { pos2=line2.find(' ', pos1); s2.insert(line2.substr(pos1, pos2-pos1)); pos1 = pos2+1; } pos1 = 0; for (int i = 0; i < n; ++i) { pos2 = line1.find(' ', pos1); string temp = line1.substr(pos1, pos2 - pos1); if (s2.find(temp) != s2.end()) s1.insert(temp); pos1 = pos2+1; } int sum = 0; for (set<string>::iterator it = s1.begin(); it != s1.end(); ++it) { int len = (*it).size(); sum += len*len; } cout << sum << endl; }
package english; import java.util.*; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()) { int n = in.nextInt(); int m = in.nextInt(); List<String> systemWords = new ArrayList<>(); Set<String> writeWordsSet = new HashSet<>(); List<String> writeWordsList = new ArrayList<String>(); for(int i = 0; i < n; i++) { systemWords.add(in.next()); } for(int i = 0; i < m; i++) { String str = in.next(); if(systemWords.contains(str)) { writeWordsSet.add(str); } } writeWordsList.addAll(writeWordsSet); int sum = 0; for(int i = 0; i < writeWordsList.size(); i++) { int len = writeWordsList.get(i).length(); sum += Math.pow(len, 2); } System.out.println(sum); } in.close(); } }
把能记住的单词丢进set里,可以去除重复单词,合理利用set的insert函数,然后判断每一个是不是在系统提供的集合里即可。
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
//为了避免重复,将单词放入set里
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
set<string> str1, str2;
for (int i = 0; i<n; i++)
{
//读入小易记住的单词
string str;
cin >> str;
str1.insert(str);
}
for (int i = 0; i<m; i++)
{
//读入系统提供的单词
string str;
cin >> str;
str2.insert(str);
}
int ans = 0;
for (set<string>::iterator it = str1.begin(); it != str1.end(); it++)
{
if (str2.find(*it) != str2.end())
{
//find()函数返回指向查找元素的迭代器,如果不存在返回set的end()迭代器.
ans += it->length() * it->length();//计算分数:单词长度的平方
}
}
cout << ans << endl;
}
}
#include <stdio.h> #include <string.h> #include <math.h> #define N 50 int find(char str[][N],char target[],int n); int main(){ int n,m,i,j,len=0,score=0; char remember[N][N],game[N][N]; char temp[N][N]; scanf("%d %d",&n,&m); for(i=0;i<n;i++){ scanf("%s",remember[i]); } for(i=0;i<m;i++){ scanf("%s",game[i]); } for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(strcmp(remember[i],game[j])==0){ if(!find(temp,remember[i],len)){ score+=pow(strlen(remember[i]),2); strcpy(temp[len++],remember[i]); } } } } printf("%d",score); printf("\n"); return 0; } int find(char str[][N],char target[],int n){ int i; for(i=0;i<n;i++){ if(strcmp(str[i],target)==0){ return 1; } } return 0; }
# 这个题的测试用例有一些问题,work around的方法是将多输入的单词截断 import sys import math class Solution: def get_max_score(self, r_words, a_words): scored_words = set(r_words) & set(a_words) total_scores = 0 for word in scored_words: total_scores += int(math.pow(len(word), 2)) print(total_scores) if __name__ == '__main__': n, m = map(int, sys.stdin.readline().strip().split()) remembered = sys.stdin.readline().strip().split()[:n] all_words = sys.stdin.readline().strip().split()[:m] solution = Solution() solution.get_max_score(remembered, all_words)
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//记住的单词个数
int m = sc.nextInt();//系统提供的单词个数
Set<String> setn = new HashSet<>();
for(int i = 0; i < n; i++)setn.add(sc.next());
Set<String> setm = new HashSet<>();
for(int i = 0; i < m; i++) setm.add(sc.next());
int len = 0;
for(String s : setn) {
if(setm.contains(s))len += s.length() * s.length();
}
System.out.println(len);
}
}
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
Set<String> set = new HashSet<>();
Set<String> words = new HashSet<>();
for (int i = 0; i < n; i++) {
String tmp = scanner.next();
set.add(tmp);
}
for (int i = 0; i < m; i++) {
String tmp = scanner.next();
words.add(tmp);
}
int grades = 0;
for (String word :
set) {
if (words.contains(word)) {
grades += word.length() * word.length();
}
}
System.out.println(grades);
}
}
num = input()
#include<iostream> #include<vector> #include<string> #include<set> using namespace std; int main() { int n,m,score = 0; cin>>n>>m; vector<string> ans(n, string()); for(int i=0;i<n;i++) cin>>ans[i]; set<string> cor,si; for(int i=0;i<m;i++) { string s; cin>>s; cor.insert(s); } for(int i=0;i<n;i++) if(cor.find(ans[i])!=cor.end()&&si.find(ans[i])==si.end()) { score+=ans[i].size()*ans[i].size(); si.insert(ans[i]); } cout<<score; }