HashMap

hdu 题目链接 点击打开链接

统计元音

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 71893    Accepted Submission(s): 28513


Problem Description
统计每个元音字母在字符串中出现的次数。
 

Input
输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。
 

Output
对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。

请特别注意:最后一块输出后面没有空行:)
 

Sample Input
  
  
2 aeiou my name is ignatius
 

Sample Output
  
  
a:1 e:1 i:1 o:1 u:1 a:2 e:1 i:3 o:0 u:1
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;


public class Main {
	
	public static void main(String[] args){
		new Task().solve();
	}
}

class Task{
	InputReader in = new InputReader(System.in) ;  
    PrintWriter out = new PrintWriter(System.out) ;  
    
    Map<Character, Integer> gao(char[] s){
    	Map<Character, Integer> m = new HashMap<Character, Integer>() ;
        for(char c : s){
        	if(! m.containsKey(c)){
        		m.put(new Character(c), 1) ;
        	}
        	else {
        		m.put(new Character(c), 1 + m.get(new Character(c))) ;
        	}
        }
    	return m ;
    }
    
 

    
	void solve(){
		
		int n = in.nextInt() ;
		while(n-- > 0){
			String s = in.nextLine() ;
			char[ ]  c = s.toCharArray() ;
			Map<Character , Integer> mm = gao(c) ; 
			int a ,e , i , o , u ;
			if(!mm.containsKey('a')){
				a = 0 ;
			}else{
				a = mm.get('a') ;
			}
			if(!mm.containsKey('e')){
				e = 0 ;
			}else{
				e = mm.get('e') ;
				
			}
			if(!mm.containsKey('i')){
				i = 0 ;
			}else{
				i = mm.get('i') ;
			}
			 if(!mm.containsKey('o')){
				 o = 0 ;
			 }else{
				 o = mm.get('o') ;
			 }
			 if(!mm.containsKey('u')){
				 u = 0  ;
			 }else{
				 u = mm.get('u');
			 }
			 out.println("a:" + a);
			 out.println("e:" + e);
			 out.println("i:" + i);
			 out.println("o:" + o);
			 out.println("u:" + u);
			 if(n != 0){
				 out.println();
			 }
		}
		
		out.flush() ;
	}
}

class InputReader {  
    public BufferedReader reader;  
    public StringTokenizer tokenizer;  
  
    public InputReader(InputStream stream) {  
        reader = new BufferedReader(new InputStreamReader(stream), 32768);  
        tokenizer = new StringTokenizer("");  
    }  
  
    private void eat(String s) {  
        tokenizer = new StringTokenizer(s);  
    }  
  
    public String nextLine() {  
        try {  
            return reader.readLine();  
        } catch (Exception e) {  
            return null;  
        }  
    }  
  
    public boolean hasNext() {  
        while (!tokenizer.hasMoreTokens()) {  
            String s = nextLine();  
            if (s == null)  
                return false;  
            eat(s);  
        }  
        return true;  
    }  
  
    public String next() {  
        hasNext();  
        return tokenizer.nextToken();  
    }  
  
    public int nextInt() {  
        return Integer.parseInt(next());  
    }  
  
    public long nextLong() {  
        return Long.parseLong(next());  
    }  
  
    public double nextDouble() {  
        return Double.parseDouble(next());  
    }  
  
    public BigInteger nextBigInteger() {  
        return new BigInteger(next());  
    }  
  
}  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值