hihocoder1385 A Simple Job JAVA String split 字符串切割

安卓使用无障碍服务监听微信和QQ的收款信息 导读: 资深的安卓程序员想必都了解,安卓的通知监听服务(NotificationListenerService)可以监听通知栏的信息,从通知栏信息里获取到我们想要的收款信息(比如收款类型、收款金额)。但是,这个通知监听服务有个弊端,如果APP没有发送通知,那就没办法知道有没有收到款,特别是现在的微信 和QQ,二维码收款不再发送通知,而是在自身的APP里给出提示,这样就没办法使用通知监听服务了。因此,我们需要使用安卓的无障碍服务(AccessibilityService),来监听APP界面的变化,实时获取支 阅读详情

#1385 : A Simple Job

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

Institute of Computational Linguistics (ICL), Peking University is an interdisciplinary institute of science and liberal arts, it focuses primarily on the fundamental researches and applications of language information processing. The research of ICL covers a wide range of areas, including Chinese syntax, language parsing, computational lexicography, semantic dictionaries, computational semantics and application systems.

Professor X is working for ICL. His little daughter Jane is 9 years old and has learned something about programming. She is always very interested in her daddy's research. During this summer vacation, she took a free programming and algorithm course for kids provided by the School of EECS, Peking University. When the course was finished, she said to Professor X: "Daddy, I just learned a lot of fancy algorithms. Now I can help you! Please give me something to research on!" Professor X laughed and said:"Ok, let's start from a simple job. I will give you a lot of text, you should tell me which phrase is most frequently used in the text."

Please help Jane to write a program to do the job.

输入

There are no more than 20 test cases.

In each case, there are one or more lines of text ended by a line of "####". The text includes words, spaces, ','s and '.'s. A word consists of only lowercase letters. Two adjacent words make a "phrase". Two words which there are just one or more spaces between them are considered adjacent. No word is split across two lines and two words which belong to different lines can't form a phrase. Two phrases which the only difference between them is the number of spaces, are considered the same.

Please note that the maximum length of a line is 500 characters, and there are at most 50 lines in a test case. It's guaranteed that there are at least 1 phrase in each test case.

输出

For each test case, print the most frequently used phrase and the number of times it appears, separated by a ':' . If there are more than one choice, print the one which has the smallest dictionary order. Please note that if there are more than one spaces between the two words of a phrase, just keep one space.

样例输入
above,all ,above all good at good at good
at good at above all me this is
####
world hello ok
####
样例输出
at good:3
hello ok:1

一看就想用split,但是交一发WA,看了两遍,没错啊,一定是连续空格出了问题,郁闷之时写了个小测试程序


import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(true) {
			String s = in.nextLine();
			String ss[] = s.split(" ");	
			System.out.println("ok");
			ArrayList<String> ls = new ArrayList<String>();
			Collections.addAll(ls, ss);
			System.out.println(ls.size());
			for (int i = 0; i < ls.size(); i++) {
				System.out.print(ls.get(i) + "*");
			}
			System.out.println("");
			
			for (int i = 0; i < ls.size(); i++) {
				String string = ls.get(i);
				if (string.isEmpty()) {
					ls.remove(i);
					i--;
				}
			}
			System.out.println(ls.size());
			for (int i = 0; i < ls.size(); i++) {
				String string = ls.get(i);
				if (string.isEmpty()) {
					System.out.print("Warning!");
				}
				System.out.print(string + "*");
				
			}
			System.out.println("");
		}
	}
}




发现两个问题,一个是诡异的******

debug一看,split在切割连续空格的时候会形成空串……要在程序中外加判断,必要的话要去除

第二个是remove之后al.size()减小了……我还以为al.size()一直是固定的……

改改就过了……JAVA还是用的不熟……


AC代码:

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String str;
		String tss[][] = new String[505][];
		Map<String, Integer> m = new TreeMap<String, Integer>(); //TreeSet自动字典序排序
		while (in.hasNextLine()) {
			String ans = ""; //用于最后输出频度最高短语
			int cnt = -1;    //频度最高短语的出现次数
			while ((str = in.nextLine()) != null) {
				if (str.charAt(0) == '#') {
					Iterator<String> it = m.keySet().iterator();
					while (it.hasNext()) {
						String key = it.next().toString();
						Integer v = m.get(key);
						if (v > cnt) {
							cnt = v;
							ans = key;
						}
					}
					m.clear();
					break;
				}
				String ss[] = str.split(",|\\."); // 以","和"."分割str
				for (int i = 0; i < ss.length; i++) {
					tss[i] = ss[i].split(" "); // 再以空格分隔一次,注意分隔时连续的空格分割会形成空字符串
				}
				for (int i = 0; i < ss.length; i++) {
					String t;
					if (tss[i].length == 1) {
						t = tss[i][0];
						if (!t.equals("")) { //如果是空字符串则忽略
							if (m.get(t) == null) {
								m.put(t, 1);
							} else {
								m.put(t, m.get(t) + 1);
							}
						}
					} else {
						ArrayList<String> al = new ArrayList<String>();
						Collections.addAll(al, tss[i]); // 将tss[i]放到al中,用于去除空字符串
						// debug j < al.size(),不是int lenal = al.size()...j < lenal,al的大小一直在变
						for (int j = 0; j < al.size(); j++) {
							if (al.get(j).isEmpty()) {
								al.remove(j);
								j--; // 注意下标变化,删除一个元素以后相当于j加了1
							}
						}
						int lenal = al.size();
						for (int j = 1; j < lenal; j++) {
							t = al.get(j - 1) + " " + al.get(j);
							if (m.get(t) == null) {
								m.put(t, 1);
							} else {
								m.put(t, m.get(t) + 1);
							}
						}
					}
				}
			}
			System.out.printf("%s:%d\n", ans, cnt);
		}
		in.close();
	}
}


Win上通过Jconsole查看Java程序资源占用情况(教程总结,一篇就够了) JConsole 是一个内置 Java 性能分析器,Java 5开始 引入了 JConsole。可以从命令行或在 GUI shell 中运行。您可以轻松地使用 JConsole来监控 Java 应用程序性能和跟踪 Java 中的代码。Jconsole工具查看程序的资源占用请求。安装jdk时bin目录有jconsole.exe工具,或者通过 Win + R,输入jconsole.exe启动jconsole( 可以从命令行直接输入jconsole。 阅读详情

相关推荐

给汽车工程师的TSN实战手册:用TSN Box和TSNTools搞定车载以太网时间同步

本文为汽车工程师提供TSN(时间敏感网络)实战指南,详细讲解如何利用TSN Box和TSNTools实现车载以太网时间同步。内容涵盖测试环境搭建、802.1AS协议配置、七种延时测量与故障诊断,以及智能驾驶域控制器的同步调试案例,帮助工程师快速掌握TSN技术并应用于实际项目。

weixin_30613343的博客 223

Android:TextUtils类介绍以及常见使用

对于字符串处理Android为我们提供了一个简单实用的TextUtils类,如果处理比较简单的内容不用去思考正则表达式不妨试试这个在android.text.TextUtils的类,主要的功能如下:  //是否为空字符  Log.e("textUtils+isEmpty", "******" + TextUtils.isEmpty("12344556"));    Log

ysh06201418的专栏 2688

Windows系统下Mamba环境配置实战:从零到编译成功

本文详细记录了在Windows系统下从零开始配置Mamba深度学习环境的完整实战过程。针对官方不直接支持Windows的难点,文章提供了关键解决方案,包括安装特定版本的Python、PyTorch与第三方Triton,以及修改源码以解决Windows编译器的兼容性问题,最终成功编译并运行Mamba-SSM。

weixin_28842269的博客 510

InputMethodManagerService 学习摘要

InputMethodManagerService 学习摘要 类的概述:接收屏幕开关、系统关闭对话框事件,启动、关闭输入法service,弹出选择输入法的对话框等等。 (1)SimpleStringSplitter TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleS

网事蒙尘 3557

Android Profiler 帧率优化实战与性能调优

本文详细介绍了使用Android Profiler进行帧率优化与性能调优的实战方法。通过分析主线程状态、FPS曲线和渲染阶段耗时,精准定位卡顿根源,并提供减少过度绘制、优化绘制命令及改善资源使用的具体策略。文章结合电商、游戏等真实案例,帮助开发者有效解决卡顿问题,提升应用流畅度。

palm99的专栏 548

Android perfetto 简介

android linux 性能优化 原理及操作

求变,从思想开始 3500

hihoCoder #1385 : A Simple Job 【2016北京网赛】模拟

题目链接:#1385 这个题比1383好理解,也好过,因为不需要那么纠结的数据结构,随便暴力搞就好(但是一个模拟有80%的AC率你信不信) 题意:找到由空格分隔的两个单词(连续的多个空格当作一个),然后对这些单词都进行计数。 需要求的是出现次数最多的的格式的词组,而且要输出出现次数 首先想到的就是:暴力模拟 枚举每一个空格(后面连续的用另外的指针选择忽略掉) 然后用一个

ACdream 636

Angular项目内存优化策略

最近,我购买了一台新的设备,期望能够显著提升工作效率,特别是在运行Angular项目时。然而,尽管设备配置先进,我仍然遇到了内存不足的错误。今天,我将分享如何解决这一问题,以及一些在Angular项目中优化内存使用的方法。

m0_74337424的博客 180

A Simple Job 模拟+multiset

题目3 : A Simple Job 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Institute of Computational Linguistics (ICL), Peking University is an interdisciplinary institute of science and liberal arts, it focuses p

STILLxjy 761

智慧加油站小程序数据库设计文档

【代码】智慧加油站小程序数据库设计文档。

低代码布道师的博客 1410

Simulink开发项1000例实战专栏--实例161:基于Simulink实现电动汽车电机驱动系统的建模与仿真

通过上述步骤,我们成功实现了基于Simulink的电动汽车电机驱动系统的建模与仿真。该系统能够全面评估电机驱动系统的性能,验证控制策略的有效性,并通过优化设计提高车辆的动力性和效率。引入智能预测:结合人工智能技术,实现更智能的控制策略。扩展功能:增加对多种电机类型的支持,提升系统通用性。实验验证:将仿真模型应用于实际硬件,进行实验验证,评估其在实际工况下的表现。

xiaoheshang_123的博客 195

hihocoder 1385 A Simple Job 模拟

题解: 从每个单词的首位开始枚举,再判断之后是否含有“,”和“.”,构成的词组和出现次数用map来存。 #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; int

关于完全听不懂神犇在说什么的事 449

【VoxCPM2】在Windows上从零搭建VoxCPM2 语音生成和语音克隆AI算法

建议你了解清楚VoxCPM2,再来考虑是否需要自己在本地搭建。

AnChenliang_1002的博客 772

android开启辅助功能拿不到,Android检测辅助功能是否开启

// To check if service is enabledprivate boolean isAccessibilitySettingsOn(Context mContext) {int accessibilityEnabled = 0;final String service = getPackageName() + "/" + YOURAccessibilityService.clas...

weixin_34258386的博客 731

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

elastic-job之Simple类型作业实现

1.什么是Simple类型作业?Simple类型作业意为简单实现,未经任何封装的类型。需实现SimpleJob接口。该接口仅提供单一方法用于覆盖,此方法将定时执行。与Quartz原生接口相似,但提供了弹性扩缩容和分片等功能。2. 创建Simple类型项目,通过API启动方式调用2.1 首先我们maven创建一个普通的java项目,然后在pom文件中加入下面这行引入&lt;dependency&gt...

一不小心的程序员 1万+

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Elastic-job系列(二)-------- simple类型作业

一、简介 esjob的作业类型有三种,分别是Simple、Dataflow和Script。本文主要介绍基于springboot的simple类型的作业。以及控制台的使用。作业名称为simpleJobDemo. 二、Simple作业 2.0 结构图 2.1 pom.xml <?xml version="1.0" encoding="UTF-8"?>...

请叫我猿叔叔的博客 6331

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

获取开启的辅助服务列表

static final char ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR = ':'; // Auxiliary members. final static SimpleStringSplitter sStringColonSplitter = new SimpleStringSplitter(ENABLED_ACCESSIBILITY_SERVI

chengjiamei的专栏 1249

RK3326 Android 8.1 为自己的APK的服务默认开启无障碍权限,不会有任何的弹窗

首先致谢 lancelots: 因为自己不是科班,所以很多东西都来自于网友的博客,非常感谢网友的分享,和快速的回复。 在能开机就运行的代码的地方加入这一块代码 // 这是获取服务列表的函数,被下面的函数调用 public Set<ComponentName> getEnabledServices(Context context) { final String e...

开发界里的一股清流 4654

Android 检测辅助功能是否开启,并调整设置页面

/** * 检测辅助功能是否开启 * 方 法 名:isAccessibilitySettingsOn * 创 建 人 * 创建时间:2016-6-22 下午2:29:24 * 修 改 人: * 修改日期: * @param mContext * @return boolean */ private boolean isAccessibilitySet

燃烧那滴泪的专栏 1万+

SimpleJob的使用实例

quartz使用的是1.8.6版本[code="java"]import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class SimpleJob implements Jo...

iteye_2216的博客 2213

java中定时任务Job的使用总结

Job的原理,参考: http://www.cnblogs.com/Dorae/p/9357180.html job的使用,参考: https://blog.csdn.net/u010996565/article/details/78591054 https://blog.csdn.net/hfut_wowo/article/details/64434664 实例,创建一个job,每天凌晨3点执行...

weixin_43751710的博客 1万+

深入了解AccessibilityService

微信红包自打出世以来就极其受欢迎,抢红包插件可谓红极一时.今天,我们重新谈谈抢红包插件的哪些事儿.

Floating Cat 3万+

Quartz2.2.1在JAVA中的简单用法(一)

由于项目中需要用到定时执行任务,后来查资料发现有Quartz这么一个任务调度框架,但是网上的大部分资料几乎都是之前的版本,Quartz-2.2.1好像经过了改版,与先前的版本在用法上有那么一些出入,2.2.1里面好多方法都被弃用了,然后经过一番折腾,算是会简单的使用了。下面简单的介绍一下:

hu1991die的专栏 8532

【字符串处理 && 字典树】HihoCoder - 1385 A Simple Job

Problem Description 给你一个文本,文本以#结束 问你出现最多的词组是什么,词组定义:连续的两个单词中间只有空格,组合成的就叫做词组 例:above,all ,above all good at good 词组有 above all, all good, good at, at good 问你文本中出现最多的词组是那个,还有出现的次数,如果次数一样

ACMer 梁剑锋 的博客 294

C A Simple Job

题目3 : A Simple Job时间限制:1000ms单点时限:1000ms内存限制:256MB描述Institute of Computational Linguistics (ICL), Peking University is an interdisciplinary institute of science and liberal arts, it focuses primarily ...

weixin_30955341的博客 164

判断AccessibilityService是否已经启用

Note : Change value of final String service = "com.test.package.name/com.test.package.name.YOURAccessibilityService" with your Service.  // To check if service is enabled private boolean isAccessib

安卓世界库 5963
上一篇: hihocoder1383 The Book List 字典树
下一篇: UVa11997 K Smallest Sums 归并 + 优先队列
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址