首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >HDOJ 2072 单词数

HDOJ 2072 单词数

作者头像
谙忆
发布2021-01-21 11:02:44
发布2021-01-21 11:02:44
3710
举报
文章被收录于专栏:程序编程之旅程序编程之旅

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

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

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

Sample Input you are my friend #

Sample Output 4

我的思路是先按照空格来分配单词。 用String数组。 最后来比较有多少个相等的单词。 相等就减一。 我用了一个boolean型的数组来判断当前单词是否已经被比较了。

代码语言:javascript
复制
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            if(str.charAt(0)=='#'){
                return ;
            }

            String[] strs = new String[str.length()];
            for(int i=0;i<strs.length;i++){
                strs[i] = "";
            }
            int count =0;
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)!=' '){
                    //System.out.println("--- "+str.charAt(i));
                    strs[count]=strs[count]+str.charAt(i);
                }else{
                    count++;
                }
            }

//          for(int i=0;i<count;i++){
//              System.out.println(strs[i]);
//          }

            int number =strs.length-1;
            boolean iscp[] = new boolean[strs.length];
            for(int i=0;i<iscp.length;i++){
                iscp[i]=false;
            }
            for(int i=0;i<strs.length-1;i++){
                for(int j=i+1;j<strs.length;j++){
                    if(strs[i].equals(strs[j])&&!iscp[j]){
                        number--;
                        iscp[j]=true;
                    }
                }
            }

            System.out.println(number);


        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/03/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档