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();
	}
}


CSAPP performance lab CSAPP 深入理解计算机系统的performance Lab 阅读详情

相关推荐

数字逻辑EduCoder课设项目 厨房计时器系统设计 Logisim实现

数字逻辑EduCoder课设项目 厨房计时器系统设计 Logisim实现

weixin_51693702的博客 1万+

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

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

ysh06201418的专栏 2687

恶意代码分析实战——使用Apatedns和Inetsim模拟网络环境

恶意代码分析实战——使用Apatedns和Inetsim模拟网络环境 注:本次实验主要是《恶意代码分析实战》一书中进入动态恶意代码分析篇章的开始,主要通过Apatedns以及Inetsim来模拟为恶意代码提供其需要的网络环境,本次实验以Lab01-03.exe为例。 一、实验环境及工具。 Vmware虚拟机:win10(win7)、kali 主要工具:Apatedns、Inetsim 二、工具的安装下载 1、Apatedns:需安装至windows虚拟机客户端(win10,win7均可) 下载地址:http

妙蛙种子吃了都会妙妙秒的妙脆角的博客 7934

InputMethodManagerService 学习摘要

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

网事蒙尘 3552

Proxmox VE安装卡顿深度解析:显卡驱动与BIOS设置的黄金法则

本文深度解析Proxmox VE安装卡顿的根本原因,指出显卡驱动与BIOS设置是两大关键因素。文章详细阐述了如何通过添加`nomodeset`内核参数解决显卡兼容性问题,并提供了针对不同主板品牌的BIOS显卡优先级设置黄金法则。同时,还分享了从预处理到高级调试的完整实战排查流程,帮助用户从源头杜绝安装卡顿,确保PVE系统顺利部署。

weixin_29278805的博客 471

告别迷茫!用ETAS RTA-OS配置你的第一个AUTOSAR OS任务(含VRTA虚拟ECU实战)

本文详细介绍了如何使用ETAS RTA-OS工具链在VRTA虚拟ECU上配置和运行第一个AUTOSAR OS任务。通过从XML配置到任务调度的全流程实战,帮助开发者快速掌握嵌入式操作系统开发的核心步骤,特别适合刚接触AUTOSAR的开发者快速上手。

weixin_42533622的博客 418

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

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

ACdream 633

嵌入式STM32控制电梯面板

嵌入式STM32控制电梯面板 用点阵来显示电梯向上还是向下运动,用数码管来显示楼层数(由于灯阵显示楼层数比较困难故用数码管代替)。当按下对应的按钮,就可以去到对应层数,可上升可下降。 #include "stm32f10x.h" #include "stm32f10x_exti.h" #include "stm32f10x_it.h" #define uint unsigned int #define KEY1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_10) #define

code_init_的博客 2110

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

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

[关系图谱] 二.Gephi导入共线矩阵构建作者关系图谱

本文主要讲解Gephi绘制作者间的关系图谱,该软件可以广泛应用于社交网络、知识图谱分析,推荐读者使用。这是非常基础的一篇文章,重点讲解Gephi使用方法,希望对大家有所帮助。

杨秀璋的专栏 1万+

PCL 基于法线微分(DoN)的点云分割【2026最新版】

基于法线微分的分割。博客长期更新,本文最新更新时间为:2026年5月10日。

点云侠的博客 39万+

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 757

使用Transformer实现自动调制识别(RML2018)

image_size = 1024 # resize重置输入大小patch_size = 16 # 从输入图片中将被提取的块大小# 分成 num_patches 个块num_patches = (image_size // patch_size) #(72/6)**2 横着裁剪),],return x})

QAQIknow的博客 1320

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

Statistics and Machine Learning Toolbox

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

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

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

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

ACMer 梁剑锋 的博客 293

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

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

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

hu1991die的专栏 8530

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

深入了解AccessibilityService

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

Floating Cat 3万+

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

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

weixin_34258386的博客 729

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

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

开发界里的一股清流 4651

elastic-job之Simple类型作业实现

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

一不小心的程序员 1万+

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是否已经启用

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

安卓世界库 5962

获取开启的辅助服务列表

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

chengjiamei的专栏 1248

Elastic-job三种模式(JAVA-API实现)

Elastic-Job java-api方式实现 为什么要用定时任务? 1.数据备份每天凌晨 每天凌晨2点,因为凌晨的时候系统不是繁忙,不使用定时任务,就是用人工操作 2.心跳的监控,每隔10分钟去监听接口,是否正常,没有定时任务需要人工去查一下 3.每隔5分钟去淘宝京东抓取订单,有些卖家在不同的平台上架,总不能商家去每隔平台定时自己拉取一下吧 4.订单的支付,由于用户30分钟没有付款,自动取消该订单 是企业应用系统中必要的,是必须要的 解决掉了,大量的人力和物力 ,SpringTask 支持单点

qq1071643762的博客 1370

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

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

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

请叫我猿叔叔的博客 6329
上一篇: 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、付费专栏及课程。

余额充值