hdu 单词数
来源:互联网 发布:演唱会门票制作软件 编辑:程序博客网 时间:2024/05/17 22:25
单词数
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 1
Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend#
Sample Output
4
方法一:字典树
import java.util.Scanner;public class HduTest2 {public static Node root = null;public static int count = 0;public static void main(String[] args) {Scanner sc = new Scanner(System.in);String str = "";while(sc.hasNextLine()) {str = sc.nextLine();if("#".equals(str)) {break;}root = new HduTest2().new Node();count = 0;String[] strArr = str.split("\\s+");//两个单词之间不一定是一个空格for(int i=0; i<strArr.length; i++) {if(strArr[i].length() != 0) {//注意1:如果每一行的最前面有空格,就会截出一个长度为0的字符串createTree(strArr[i]);}}System.out.println(count);}}private static void createTree(String str) {Node tempNode = root;int ch;for(int i=0; i<str.length(); i++) {ch = str.charAt(i)-'a';if(tempNode.arr[ch] == null) {tempNode.arr[ch] = new HduTest2().new Node();}tempNode = tempNode.arr[ch];}if(tempNode.end != true) {tempNode.end = true;count ++;}}class Node {Node[] arr = new Node[26];boolean end = false;public Node() {for(int i=0; i<26; i++) {arr[i] = null;}}}}
方法二:集合法(简单,容易实现)
import java.util.HashSet;import java.util.Scanner;import java.util.Set;public class HduTest3 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String str = "";while(sc.hasNextLine()) {str = sc.nextLine();if("#".equals(str)) {break;}Set<String> set = new HashSet<String>();String strArr[] = str.split("\\s+");for(int i=0; i<strArr.length; i++) {if(strArr[i].length() != 0) {set.add(strArr[i]);}}System.out.println(set.size());}}}
- HDU 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- Hdu 2072 - 单词数
- Hdu 2072 单词数
- hdu 2072-单词数
- hdu 单词数
- hdu 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- HDU 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- hdu 2072 单词数
- HDU 2072 单词数
- hdu 2082 单词数
- Xcode调试攻略(5种方法)
- android中三种onClick事件的实现,与对比
- 无路由器情况下多设备使用网络,connectify软件的支持
- JSONKit
- 关于 DataList 、gridview 、 repeter 点击当前行的按钮 获取当前行的某个值
- hdu 单词数
- shell 与 sqlplus 交互
- ZOJ 1354 Extended Lights Out(枚举)
- 双向口 与 准双向口 区别
- 计算机是如何启动的?
- 腾讯抄你肿么办
- 在程序退出时自动清理资源-atexit
- 如何使用Nginx配置虚拟服务器和做负载均衡
- Java Socket编程