news 2026/6/2 21:06:21

(新卷,100分)- 掌握的单词个数(Java JS Python C)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(新卷,100分)- 掌握的单词个数(Java JS Python C)

(新卷,100分)- 掌握的单词个数(Java & JS & Python & C)

题目描述

有一个字符串数组 words 和一个字符串 chars。

假如可以用 chars 中的字母拼写出 words 中的某个“单词”(字符串),那么我们就认为你掌握了这个单词。

words 的字符仅由 a-z 英文小写字母组成,例如 "abc"

chars 由 a-z 英文小写字母和 "?" 组成。其中英文 "?" 表示万能字符,能够在拼写时当作任意一个英文字母。例如:"?" 可以当作 "a" 等字母。

注意:每次拼写时,chars 中的每个字母和万能字符都只能使用一次。

输出词汇表 words 中你掌握的所有单词的个数。没有掌握任何单词,则输出0。

输入描述

第一行:输入数组 words 的个数,记作N。

第二行 ~ 第N+1行:依次输入数组words的每个字符串元素

第N+2行:输入字符串chars

输出描述

输出一个整数,表示词汇表 words 中你掌握的单词个数

备注
  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length, chars.length ≤ 100
  • 所有字符串中都仅包含小写英文字母、英文问号
用例
输入4
cat
bt
hat
tree
atach??
输出3
说明可以拼写字符串"cat"、"bt"和"hat"
输入3
hello
world
cloud
welldonehohneyr
输出2
说明可以拼写字符串"hello"和"world"
输入3
apple
car
window
welldoneapplec?
输出2
说明可以拼写字符串"apple"和“car”
题目解析

本题可以分别统计出chars和word中各字符的数量,然后遍历word每个字符c,比较word和chars中统计的c字符数量,如果word的c数量超过了chars的c数量,那么就就将超出数量计入diff中。

最终比较diff和chars中万能字符‘?’的数量,如果chars中万能字符‘?’的数量 >= diff,那么说明chars可以使用万能字符补足不同部分,即可以学会word。

JS算法源码
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { const n = parseInt(await readline()); const words = []; for (let i = 0; i < n; i++) words.push(await readline()); const chars = await readline(); console.log(getResult(words, chars)); })(); function getResult(words, chars) { let ans = 0; // 统计chars字符串中各字符的数量 const cnt_chars = charStatistic(chars); for (let word of words) { let diff = 0; // 统计word字符串中各字符的数量 const cnt_word = charStatistic(word); for (let j = 0; j < 128; j++) { // 记录word的某字符超过chars的对应字符出现的数量 diff += Math.max(cnt_word[j] - cnt_chars[j], 0); } if (diff <= cnt_chars["?".charCodeAt()]) { ans++; // console.log(word); } } return ans; } function charStatistic(s) { const cnts = new Array(128).fill(0); for (let c of s) { cnts[c.charCodeAt()]++; } return cnts; }
Java算法源码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] words = new String[n]; for (int i = 0; i < n; i++) { words[i] = sc.next(); } String chars = sc.next(); System.out.println(getResult(words, n, chars)); } public static int getResult(String[] words, int n, String chars) { int ans = 0; // 统计chars字符串中各字符的数量 int[] cnt_chars = charStatistic(chars); for (int i = 0; i < n; i++) { int diff = 0; // 统计word字符串中各字符的数量 int[] cnt_word = charStatistic(words[i]); for (int j = 0; j < 128; j++) { // 记录word的某字符超过chars的对应字符出现的数量 diff += Math.max(cnt_word[j] - cnt_chars[j], 0); } if (diff <= cnt_chars['?']) { ans++; // System.out.println(words[i]); } } return ans; } public static int[] charStatistic(String s) { int[] cnts = new int[128]; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); cnts[c] += 1; } return cnts; } }
Python算法源码
# 输入获取 n = int(input()) words = [] for i in range(n): words.append(input()) chars = input() def charStatistic(s): cnts = [0] * 128 for c in s: cnts[ord(c)] += 1 return cnts # 算法入口 def getResult(): ans = 0 # 统计chars字符串中各字符的数量 cnt_chars = charStatistic(chars) for word in words: diff = 0 # 统计word字符串中各字符的数量 cnt_word = charStatistic(word) for j in range(128): # 记录word的某字符超过chars的对应字符出现的数量 diff += max(cnt_word[j] - cnt_chars[j], 0) if diff <= cnt_chars[ord('?')]: ans += 1 # print(word) return ans # 算法调用 print(getResult())
C算法源码
#include <stdio.h> #include <stdlib.h> #define MAX(a,b) ((a) > (b) ? (a) : (b)) int* charStatistic(const char* s) { int* cnts = (int*) calloc(128, sizeof(int)); int i = 0; while(s[i] != '\0') { cnts[s[i]]++; i++; } return cnts; } int main() { int n; scanf("%d", &n); char words[100][101]; for (int i = 0; i < n; i++) { scanf("%s", words[i]); } char chars[101]; scanf("%s", chars); // 记录题解 int ans = 0; // 统计chars字符串中各字符的数量 int* cnts_char = charStatistic(chars); for(int i=0; i<n; i++) { int diff = 0; // 统计word字符串中各字符的数量 int* cnt_word = charStatistic(words[i]); for(int j=0; j<128; j++) { // 记录word的某字符超过chars的对应字符出现的数量 diff += MAX(cnt_word[j] - cnts_char[j], 0); } if(diff <= cnts_char['?']) { ans++; // puts(words[i]); } } printf("%d\n", ans); }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/28 21:34:21

_基于springboot的智能家居系统(11675)

有需要的同学&#xff0c;源代码和配套文档领取&#xff0c;加文章最下方的名片哦 一、项目演示 项目演示视频 二、资料介绍 完整源代码&#xff08;前后端源代码SQL脚本&#xff09;配套文档&#xff08;LWPPT开题报告&#xff09;远程调试控屏包运行 三、技术介绍 Java…

作者头像 李华
网站建设 2026/5/28 23:52:41

【完全免费】小丸工具箱超详细的视频体积压缩教程,从1G视频压缩到100M,几乎不损画质,完全免费,而且可以批量处理视频文件

——软件使用教程—— 小丸工具箱超详细的视频体积压缩教程&#xff0c;从1G视频压缩到100M——下载地址&#xff08;防止被拦截&#xff0c;请用浏览器打开&#xff09;—— 夸克地址&#xff1a; https://pan.dxlszyk.com/s/1jeou2e1k 多盘地址&#xff1a; https://www.…

作者头像 李华
网站建设 2026/5/30 13:31:47

【完全免费】黑白照片变彩色照片,一键处理百张黑白老照片,AI上色效果比手工精细10倍,效果太惊艳了,老照片AI修复上色全流程演示,支持离线使用!

——软件使用教程—— 黑白照片变彩色照片&#xff0c;一键处理百张黑白老照片&#xff0c;支持离线使用&#xff01;——下载地址&#xff08;防止被拦截&#xff0c;请用浏览器打开&#xff09;—— 夸克地址&#xff1a; https://pan.dxlszyk.com/s/1jemfj9sk 多盘地址&a…

作者头像 李华
网站建设 2026/5/28 21:34:22

程序员必备的语义检索工具:基于GTE模型的高效相似度计算实践

程序员必备的语义检索工具&#xff1a;基于GTE模型的高效相似度计算实践 在现代信息处理系统中&#xff0c;语义理解能力已成为提升搜索、推荐和问答系统智能化水平的核心要素。传统的关键词匹配方式已难以满足复杂场景下的精准需求&#xff0c;而语义检索技术正逐步成为构建智…

作者头像 李华
网站建设 2026/5/28 22:43:40

一键智能抠图实践|基于CV-UNet大模型镜像快速部署批量处理方案

一键智能抠图实践&#xff5c;基于CV-UNet大模型镜像快速部署批量处理方案 在电商产品图处理、AI图像生成、数字内容创作等场景中&#xff0c;高质量的图像抠图能力已成为基础刚需。传统手动抠图效率低&#xff0c;而市面上多数在线工具存在隐私泄露、成本高、无法批量处理等问…

作者头像 李华
网站建设 2026/5/29 0:13:32

一键批量抠图实践|基于CV-UNet大模型镜像高效实现

一键批量抠图实践&#xff5c;基于CV-UNet大模型镜像高效实现 1. 引言&#xff1a;智能抠图的工程化落地需求 在电商、广告设计、影视后期等场景中&#xff0c;图像背景移除&#xff08;即“抠图”&#xff09;是一项高频且关键的任务。传统手动抠图效率低、成本高&#xff0…

作者头像 李华