zoukankan      html  css  js  c++  java
  • WUSTOJ 1279: Wallace and His Pet(Java)

    1279: Wallace and His Pet

    题目

      给出一句话(英文),单词总数不超过1000,每个单词不超过10个字符,一句话只有一个唯一的字符“.”(句点)。将这句话中出现的最多的单词替换成“guagua”。更多内容点击标题。

    分析

    1. 不区分大小写,这很关键。
    2. 统计频率很简单,循环遍历整句话就可以。
    3. 句子末尾有个句点,我用的next()读取的字符串(单词),因此句点会和最后一个单词保存在一起。要去掉这个点。
    4. 输出最后没有空格,有句点。记得换行。

    代码

    /**
     * time 730ms
     * @author PengHao
     * @version A1.1
     * @date 2019-04-22 下午5:10:22
     */
    
    import java.util.Scanner;
    
    public class Main {
    
    	private Scanner sc;
    	private int MAX_WORD = 1001; // 最多1000个单词,下标从1开始
    	private int numOfWords; // 每行的单词个数
    	private String[] words; // 每行的单词
    	private boolean[] visited; // 访问标志,单词统计过就设为true
    	private String mostWords; // 频率最高的单词
    
    	public Main() {
    		sc = new Scanner(System.in);
    		words = new String[MAX_WORD]; // 单词
    		visited = new boolean[MAX_WORD]; // 访问标志
    		int T = sc.nextInt(); // 数据组数
    		while ((T--) > 0) {
    			input(); // 输入
    			initVisited(); // 初始化访问标志
    			mostWords = findMostWord(); // 获取频率最高的单词
    			output(); // 输出
    		}
    		sc.close();
    	}
    
    	/**
    	 * Input Words.
    	 */
    	private void input() {
    		numOfWords = 0;
    		char end; // 单词的最后一个字符
    		do {
    			words[++numOfWords] = sc.next();
    			end = words[numOfWords].charAt(words[numOfWords].length() - 1);
    		} while ('.' != end); // 不是'.',就继续输入
    		// 去掉最后一个单词的点'.'
    		words[numOfWords] = words[numOfWords].replace(".", "");
    	}
    
    	/**
    	 * Initializes the access flag.
    	 */
    	private void initVisited() {
    		for (int i = 1; i <= numOfWords; i++) {
    			visited[i] = false;
    		}
    	}
    
    	/**
    	 * @return 频率最高的单词
    	 */
    	private String findMostWord() {
    		String mostW = ""; // 频率
    		int mostNum = 0; // 初始最高频率为0
    		String currentWord; // 当前单词
    		int currentNum; // 当前单词个数
    		for (int i = 1; i <= numOfWords; i++) {
    			if (visited[i]) { // 如果这个单词已经统计过,直接跳过
    				continue;
    			}
    			currentWord = words[i].toLowerCase(); // 当前单词变成小写
    			currentNum = 1; // 当前单词出现1次
    			visited[i] = true; // 当前单词设为已经统计过
    			// 检查这个单词在后面是否出现
    			for (int j = i + 1; j <= numOfWords; j++) {
    				// 第j个单词和当前单词一样
    				if (words[j].toLowerCase().contentEquals(currentWord)) {
    					currentNum++; // 个数加1
    					visited[j] = true; // 设为已经统计过
    				}
    			}
    			if (currentNum > mostNum) { // 当前单词的频率比最高的单词频率更高
    				mostW = currentWord; // 更新最高频率单词
    				mostNum = currentNum; // 更新最高频率单词的频率
    			}
    		}
    		return mostW;
    	}
    
    	/**
    	 * Output
    	 */
    	private void output() {
    		// 前numOfWords-1个单词
    		for (int i = 1; i < numOfWords; i++) {
    			// 这个单词是最高频率的单词
    			if (words[i].toLowerCase().contentEquals(mostWords)) {
    				System.out.print("guagua "); // 用guagua替换
    			} else {
    				System.out.print(words[i] + " "); // 原样输出
    			}
    		}
    		// 最后一个单词
    		// 这个单词是频率最高的单词
    		if (words[numOfWords].toLowerCase().contentEquals(mostWords)) {
    			System.out.println("guagua."); // 用guagua替换
    		} else {
    			System.out.println(words[numOfWords] + ".");
    		}
    	}
    
    	public static void main(String[] args) {
    		new Main();
    	}
    
    }
    

    写在最后:

    1. 如需转载,请于标题下注明链接形式的wowpH的博客即可;
    2. 代码原创,如需公开引用,不能删除首行注释(作者,版本号,时间等信息)。
    3. 如果有疑问欢迎评论留言,尽力解答。

  • 相关阅读:
    Flask---框架入门
    续--Flask, Django
    测试开发中Django和Flask框架
    oracle数据库的存储原理
    Oracle 存储过程—为数传递变量
    Oracle scope中 spfile、memory、both 的区别
    数据库性能衡量指标
    raid卷性能测试
    HTTP POST请求报文格式分析与Java实现文件上传
    使用Navicat 导入导出Mysql数据库
  • 原文地址:https://www.cnblogs.com/wowpH/p/11060813.html
Copyright © 2011-2022 走看看