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();
    	}
    }
    
  • 相关阅读:
    Python 第八章笔记
    B树和B+树的总结
    哈希表总结
    Redis基本数据结构总结之STRING和LIST
    红黑树之删除原理和实现
    红黑树之插入实现
    对排名前3000位博主进行数据分析
    o(n)线性排序算法
    排序算法总结
    贪心算法 题型总结
  • 原文地址:https://www.cnblogs.com/wowpH/p/11687483.html
Copyright © 2011-2022 走看看