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();
    	}
    }
    
  • 相关阅读:
    Linux-exec族函数
    Linux-竟态初步引入
    Linux-waitpid介绍
    Java基础:Java运算符:算术运算符
    Java中的算术运算符
    JAVA冒泡排序
    引用 java的一些基本概念
    Tomcat服务器的下载安装跟基本配置
    Tomcat配置Web站点
    Tomcat+JSP经典配置实例
  • 原文地址:https://www.cnblogs.com/wowpH/p/11687483.html
Copyright © 2011-2022 走看看