单词数 (HashSet)

https://vjudge.net/contest/352887#problem/D

lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。

Input

有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。

Output

每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。

Sample Input

you are my friend
#

Sample Output

4

注意:多个空格连在一起的时候,不能用str.split(" ");

扫描二维码关注公众号,回复: 10283289 查看本文章
import java.util.*;
public class Main {
	static Scanner sc=new Scanner(System.in);
	static Set<String> se=new HashSet<String>();
	public static void main(String[] args) {
	        String str;
		while(sc.hasNext()) {
			str=sc.nextLine();
			if(str.equals("#")) break;
			String ss=str.replaceAll(" +",",");
			String[] s=ss.split(",");
			se.clear();
			for(int i=0;i<s.length;i++) {
				se.add(s[i]);
			}
			System.out.println(se.size());
		}
	}
}
发布了428 篇原创文章 · 获赞 55 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_42936517/article/details/104065776