湖北做网站教程哪家好攀枝花市三线建设博物馆网站
2026/4/2 8:49:08 网站建设 项目流程
湖北做网站教程哪家好,攀枝花市三线建设博物馆网站,手机版oa系统下载,濮阳网红(新卷,100分)- 掌握的单词个数#xff08;Java JS Python C#xff09;题目描述有一个字符串数组 words 和一个字符串 chars。假如可以用 chars 中的字母拼写出 words 中的某个“单词”#xff08;字符串#xff09;#xff0c;那么我们就认为你掌握了这…(新卷,100分)- 掌握的单词个数Java JS Python C题目描述有一个字符串数组 words 和一个字符串 chars。假如可以用 chars 中的字母拼写出 words 中的某个“单词”字符串那么我们就认为你掌握了这个单词。words 的字符仅由 a-z 英文小写字母组成例如 abcchars 由 a-z 英文小写字母和 ? 组成。其中英文 ? 表示万能字符能够在拼写时当作任意一个英文字母。例如? 可以当作 a 等字母。注意每次拼写时chars 中的每个字母和万能字符都只能使用一次。输出词汇表 words 中你掌握的所有单词的个数。没有掌握任何单词则输出0。输入描述第一行输入数组 words 的个数记作N。第二行 ~ 第N1行依次输入数组words的每个字符串元素第N2行输入字符串chars输出描述输出一个整数表示词汇表 words 中你掌握的单词个数备注1 ≤ words.length ≤ 1001 ≤ words[i].length, chars.length ≤ 100所有字符串中都仅包含小写英文字母、英文问号用例输入4catbthattreeatach??输出3说明可以拼写字符串cat、bt和hat输入3helloworldcloudwelldonehohneyr输出2说明可以拼写字符串hello和world输入3applecarwindowwelldoneapplec?输出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 i0; in; i) { int diff 0; // 统计word字符串中各字符的数量 int* cnt_word charStatistic(words[i]); for(int j0; j128; j) { // 记录word的某字符超过chars的对应字符出现的数量 diff MAX(cnt_word[j] - cnts_char[j], 0); } if(diff cnts_char[?]) { ans; // puts(words[i]); } } printf(%d\n, ans); }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询