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();
    	}
    }
    
  • 相关阅读:
    苹果手机的SB系列(9)移动版的AAP个个都比桌面版大?
    初识Redis(四)
    初识Redis(三)
    初识Redis(二)
    初识Redis(一)
    Codeforces Round #589 (Div. 2)
    康拓展开学习笔记
    最短路模板
    K
    D
  • 原文地址:https://www.cnblogs.com/wowpH/p/11687483.html
Copyright © 2011-2022 走看看