单词数
Online Judge Online Exercise Online Teaching Online Contests Exercise Author
F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
C/C++/Java Exams
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests
DIY | Web-DIY beta
Recent Contests
Author 202020044110
Mail Mail 0(0)
Control Panel Control Panel
Sign Out Sign Out
(点击查看详情)杭电ACM-LCY算法培训“进阶班”
单词数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 98750 Accepted Submission(s): 25780
Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend
Sample Output
4
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
while(in.hasNext()){
String o=in.nextLine();
HashSet<String> Words =new HashSet<String>();
if(o.equals("#")){
break;
}
String []a=o.split(" ");
for(int i=0;i<a.length;i++)
{
Words.add(a[i]);
}
if(Words.contains("")){
System.out.println(Words.size()-1);}
else
System.out.println(Words.size());
}
}
}
分析:
题目上说要不同单词的个数;所以需要去重;java中有Set集合可以去重;