首页 > 试题广场 >

小易记单词

[编程题]小易记单词
  • 热度指数:2944 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
小易参与了一个记单词的小游戏。游戏开始系统提供了m个不同的单词,小易记忆一段时间之后需要在纸上写出他记住的单词。小易一共写出了n个他能记住的单词,如果小易写出的单词是在系统提供的,将获得这个单词长度的平方的分数。注意小易写出的单词可能重复,但是对于每个正确的单词只能计分一次。

输入描述:
输入数据包括三行:
第一行为两个整数n(1 ≤ n ≤ 50)和m(1 ≤ m ≤ 50)。以空格分隔
第二行为n个字符串,表示小易能记住的单词,以空格分隔,每个单词的长度小于等于50。
第三行为m个字符串,系统提供的单词,以空格分隔,每个单词的长度小于等于50。


输出描述:
输出一个整数表示小易能获得的分数
示例1

输入

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);
	}
}


发表于 2017-09-15 09:35:18 回复(1)
/*
先将两行输入分别读入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;
}

发表于 2017-08-11 10:32:55 回复(0)
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();
	}
}

发表于 2017-04-15 20:36:27 回复(3)
import sys
n,m=list(map(int,sys.stdin.readline().strip().split()))
listn=list(sys.stdin.readline().strip().split())
listm=list(sys.stdin.readline().strip().split())
listn=list(set(listn))
res=0
for i in listn:
    if i in listm[0:m]:
        res+=len(i)**2
print(res)
发表于 2018-05-18 08:59:01 回复(0)

思路分析

把能记住的单词丢进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;
}

发表于 2017-04-11 09:08:38 回复(0)
import java.util.*;
 
public class Main{
        public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = 0;
        int m = 0;
 
        n = scanner.nextInt(); //能够记住的单词数
        m = scanner.nextInt();//系统的单词数
 
        Set<String> set = new HashSet<>();
        for(int i = 0; i < n; ++i) {
            set.add(scanner.next());
        }
 
        long result = 0;
        for(int i = 0; i < m; ++i) {
            String temp = scanner.next();
            if(set.contains(temp)) {
                result += (long)Math.pow(temp.length(), 2);
            }
        }
 
        System.out.println(result);
    }
 
}
编辑于 2017-04-03 13:48:25 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        Set<String> rWords = new HashSet<>();
        Set<String> sWords = new HashSet<>();
        for (int i = 0; i < n; i++) {
            rWords.add(scan.next());
        }
        for (int i = 0; i < m; i++) {
            sWords.add(scan.next());
        }
        int res = 0;
        for (String s : rWords) {
            if (sWords.contains(s)) {
                int len = s.length();
                res += len * len;
            }
        }
        System.out.println(res);
    }
}

发表于 2017-03-31 15:41:49 回复(0)
对于Java,这道题如果对scanner使用了循环,即如:while(scanner.hasNext())类似这样的大循环,必然无法通过,而通过的那些java代码里都是去掉了这些循环的。
我也被坑到了。。
发表于 2017-03-30 09:35:04 回复(3)
#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;
}

发表于 2017-03-30 00:38:14 回复(1)
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

public class Main{ 
    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         HashSet<String>set=new HashSet<String>();
         StringBuilder sb=new StringBuilder();
         int m,n,i,score=0;
         m=sc.nextInt();
         n=sc.nextInt();
         for(i=0;i<m;i++) 
        set.add(sc.next());
         for(i=0;i<n;i++)
        sb.append(sc.next()).append("-");
         Iterator<String> it = set.iterator();
         while(it.hasNext()){
        String temp=it.next();
        if(sb.indexOf(temp)!=-1)
        score+=(int)Math.pow(temp.length(),2);
         }
         System.out.println(score);
  }
}
发表于 2017-03-29 19:44:17 回复(0)
n, m = map(int, input().split())
print(sum(len(i) ** 2 for i in set(input().split()[:n]) & set(input().split()[:m])))


发表于 2020-07-21 17:50:15 回复(0)
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(){
    int n, m;
    while(cin >> n >> m){
        //分别记录系统提供的不等的单词,和记住的单词
        vector<string> system_words;
        vector<string> remember_words;
        for(int i = 0; i < n; i++){
            string tmp;
            cin >> tmp;
            remember_words.push_back(tmp);
        }
        for(int i = 0; i < m; i++){
            string tmp;
            cin >> tmp;
            system_words.push_back(tmp);
        }
        
        int scores = 0;
        vector<int> is_same(m,0);    //统计记住的单词和系统单词相同的次数,按提供单词建表
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(remember_words[i] == system_words[j]){
                    is_same[j]++;
                }
            }
        }
        //算总分
        for(int i = 0; i < is_same.size(); i++){
            if(is_same[i] != 0){//重复记忆则只算一次
                scores += system_words[i].size()* system_words[i].size();
            }
        }
        cout << scores << endl;
        //释放所申请的vector
        system_words.clear();
        remember_words.clear();
        is_same.clear();
    }
    return 0;
}
发表于 2019-12-05 17:15:08 回复(0)
//利用vector System存储系统单词;利用set中属性的唯一性存储记忆的单词;然后利用find函数查找即可;
#include<iostream>
#include<string>
#include<set>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;

int main()
{
    int n, m;
    int score = 0;
    string system, remember,temp;
    vector<string> System; //存储系统单词
    set<string> Remember; //属性的唯一性存储记忆的单词
    vector<string>::iterator iter;
    set<string>::iterator itera;
    cin >> n >> m; //输入
    for (int i = 0; i < n; i++)
    {
        cin >> remember;
        Remember.insert(remember);
    }
    for (int j = 0; j < m; j++)
    {
        cin >> system;
        System.push_back(system);
    }
    for (itera = Remember.begin(); itera != Remember.end(); itera++) //查找
    {   
        temp = *itera;
        iter = find(System.begin(),System.end(),*itera);
        if(iter != System.end())
            score += pow(temp.size(), 2);
    }
    cout << score << endl;
}
发表于 2019-07-24 10:40:29 回复(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)

发表于 2018-07-12 15:15:29 回复(0)
import java.util.Scanner;
 
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        String[] s1 = newString[n];
        String[] s2 = newString[m];
        for(int i=0;i<n;i++){
            s1[i] = in.next();
        }
        for(inti=0;i<m;i++){
            s2[i] = in.next();
        }
        intindex=m;
        intcount = 0;
        for(inti=0;i<n;i++){
            for(intj=0;j<index;j++){
                if(s1[i].equals(s2[j])){
                    count = count+ s1[i].length()*s1[i].length();
                    s2[j] = s2[index-1];
                    index--;
                }
            }
        }
        System.out.println(count);
    }
}
发表于 2018-05-02 15:46:32 回复(0)
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);
    }
}
发表于 2018-04-14 09:11:16 回复(0)
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);
    }
}
发表于 2018-04-11 20:41:25 回复(0)
 num = input()
memory = input()
all_word = input()
num = num.split(' ')
fir, sec = int(num[0]),int(num[1])
memory = memory.split(' ')
all_word = all_word.split(' ')
right_copy = list()
right = list()
for i in range(fir):
    if memory[i] in all_word:
        right_copy.append(memory[i])
for i in range(len(right_copy)):
    if right_copy[i] not in right:
        right.append(right_copy[i])
n = 0
for i in range(len(right)):
    n = n+len(right[i])**2

print(n)

发表于 2018-03-27 17:48:56 回复(0)
#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;
}
发表于 2018-02-06 22:40:56 回复(0)

#include <iostream>
#include "list"
#include "vector"
#include <algorithm>
#include "math.h"
#include "map"
using namespace std;

int main()
{
    int n,m;
    cin>>n>>m;
    int score = 0;
    map<string,int> _map;
    for(int i =0 ;i < n ;++i)
    {
        string s;
        cin>>s;
        _map.insert(make_pair(s,1));
    }
    
    for(int i =0 ;i < m ;++i)
    {
        string s;
        cin>>s;
        pair<map<string,int>::iterator,bool > rent =  _map.insert(make_pair(s,1));
        if(!rent.second)
        {
            
            score += rent.first->first.length() * rent.first->first.length();
        }
    }
    
    cout<<score<<endl;
    return 0;
}


发表于 2018-01-17 10:57:49 回复(0)

问题信息

难度:
45条回答 7822浏览

热门推荐

通过挑战的用户

小易记单词