zoukankan      html  css  js  c++  java
  • WUSTOJ 1321: Alphabet Cookies(Java)字符统计

    题目链接:1321: Alphabet Cookies

    Description

    Kitty likes cookies very much, and especially the alphabet cookies. Now, she get some alphabet cookies, and she wants to select some of them to spell some words.

    The easy task for you, is to determine that whether she can spell the word she wants.

    Input

    The input contains several test cases.

    Each test case contains an integer N ( 0 < N ≤ 100 ) in a line.

    And followed a line with N capital letters, means the cookies she has.

    Then the last line is a word with capital letters. And the word’s length isn’t more than N.

    Output

    One word for each test case. If she can spell the word by these letters, please output “Yes”, nor output “No”.

    Sample Input

    7
    ARDHPYP
    HAPPY
    6
    ARDHPY
    HAPPY
    

    Sample Output

    Yes
    No
    

    代码

    /**
     * Time 258ms
     * @author wowpH
     * @version 1.0
     * @date 2019年6月24日下午12:39:59
     * Environment:	Windows 10
     * IDE Version:	Eclipse 2019-3
     * JDK Version:	JDK1.8.0_112
     */
    
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(new InputStreamReader(System.in));
    		while (sc.hasNext()) {
    			int N = sc.nextInt();
    			String cookies = sc.next();
    			String word = sc.next();
    
    			int[] lettersNum = new int[26];
    			for (int i = cookies.length() - 1; i >= 0; --i) {
    				++lettersNum[cookies.charAt(i) - 'A'];// 统计每个字符的个数
    			}
    
    			int i;
    			for (i = word.length() - 1; i >= 0; --i) {
    				if (lettersNum[word.charAt(i) - 'A'] <= 0) {// 当前字符不够,退出
    					break;
    				}
    				--lettersNum[word.charAt(i) - 'A'];// 够,个数减1
    			}
    			if (i >= 0) {
    				System.out.println("No");
    			} else {
    				System.out.println("Yes");
    			}
    		}
    		sc.close();
    	}
    }
    
  • 相关阅读:
    人工智能-实验一策略迭代和值迭代
    Lecture 3: Planning by Dynamic Programming
    Lecture 2: Markov Decision Processes
    Software Testing -- LAB02-Selenium上机实验
    数据结构-王道2017-第4章 树与二叉树-二叉树的遍历
    数据结构-王道2017-第4章 树与二叉树
    PHP-基础知识
    nginx的安装与配置
    在Linux上部署项目
    zk 命令
  • 原文地址:https://www.cnblogs.com/wowpH/p/11687483.html
Copyright © 2011-2022 走看看