Give My Text Back
Give My Text Back
标签(空格分隔): 算法
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
To prepare for the English exam Little Ho collected many digital reading materials. Unfortunately the materials are messed up by a malware.
It is known that the original text contains only English letters (a-zA-Z), spaces, commas, periods and newlines, conforming to the following format:
Each sentence contains at least one word, begins with a letter and ends with a period.
In a sentence the only capitalized letter is the first letter.
In a sentence the words are separated by a single space or a comma and a space.
The sentences are separated by a single space or a single newline.
It is also known the malware changes the text in the following ways:
Changing the cases of letters.
Adding spaces between words and punctuations.
Given the messed text, can you help Little Ho restore the original text?
输入
A string containing no more than 8192 English letters (a-zA-Z), spaces, commas, periods and newlines which is the messed text.
输出
The original text.
样例输入
my Name is Little Hi.
His name IS Little ho , We are friends.
样例输出
My name is little hi.
His name is little ho, we are friends.
解析
规则:
- 每个标点符号之前没有空格,标点符号之后加空格;
- ‘.’之后表示每个句子的开始,首字母应该大写;
- 只有每个句子的句首字母应该大写;
代码:
import java.util.Scanner;
public class Main {
public static String dealwith(String s) {
String s1 = s.trim().toLowerCase();
char[] ch = s1.toCharArray();
ch[0] = Character.toUpperCase(ch[0]);
StringBuffer sb = new StringBuffer();
boolean flag = false;
for(int i=0; i<ch.length; i++) {
if( ch[i] <= 'z' && ch[i] >= 'A') { //正常的字符
if(flag == true) {
sb.append(Character.toUpperCase(ch[i]));
flag = false;
}
else
sb.append(ch[i]);
}
else if(ch[i] == ' ') { //如果是空格,分两种情况
if(sb.charAt(sb.length()-1) != ' ')
sb.append(" ");
else
continue;
}
else { //是标点符号
if(ch[i] == '.')
flag = true;
if(sb.charAt(sb.length()-1) == ' ') { //如果标点前有空格,则删除
sb.deleteCharAt(sb.length()-1);
sb.append(ch[i]);
sb.append(" ");
}
else {
if(i == ch.length-1) //如果是最后一个字符
sb.append(ch[i]);
else {
sb.append(ch[i]);
sb.append(" ");
}
}
}
}
return sb.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String s = sc.nextLine();
String res = dealwith(s);
System.out.println(res);
}
}
}
Give My Text Back的更多相关文章
- Sublime Text 3中文乱码解决方法以及安装包管理器方法
一般出现乱码是因为文本采用了GBK编码格式,Sublime Text默认不支持GBK编码. 安装包管理器 简单安装 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令 ...
- requests的content与text导致lxml的解析问题
title: requests的content与text导致lxml的解析问题 date: 2015-04-29 22:49:31 categories: 经验 tags: [Python,lxml, ...
- xpath提取多个标签下的text
title: xpath提取多个标签下的text author: 青南 date: 2015-01-17 16:01:07 categories: [Python] tags: [xpath,Pyth ...
- 在Sublime Text 3上安装代码格式化插件CodeFormatter
1.了解CodeFormatter插件 在Sublime Text 3中编写代码,为了能让我们的代码格式变得漂亮整洁,需要一个能自动格式代码的插件.这里发现CodeFormatter插件不错,它能支持 ...
- React Native 之 Text的使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- UGUI Text(Label)
环境 Unity 5.3.6f1 关于Best Fit 如果勾选了 Best Fit ,当有大量的文本填充在Text上时,那么文字是不会自动换行的. 打字机效果 在github上已有现成的:https ...
- sublime text 3 + python配置,完整搭建及常用插件安装
四年的时间,一直使用EmEditor编辑器进行Python开发,之前是做面向过程,只需要将一个单独的py文件维护好即可,用着也挺顺手,但是最近在做面向对象的开发,不同的py文件中相互关联较多,感觉单纯 ...
- 前端工程师手中的Sublime Text
原文地址:http://css-tricks.com/sublime-text-front-end-developers/ 我的Blog:http://cabbit.me/sublime-text-f ...
- Sublime Text 全程指引 by Lucida
作者:Lucida 微博:@peng_gong 豆瓣:@figure9 博客园:@figure9 原文链接:http://zh.lucida.me/blog/sublime-text-complete ...
- 自定义Sublime Text的图标
sublime text很赞,windows上最接近mac逼格的轻量编辑器,对于我这样比较喜欢格调的人来说,简直不二之选啊. 美中不足的是,看久了觉得它的图标似乎不是很上心.现在都流行扁平化了而它还停 ...
随机推荐
- linux更新系统之后,删除多余的开机启动项
实验环境是centos7,采用uefi的引导方式,启动管理软件是grub2 1. 进入 /boot 目录,应该可以发现许多文件的文件名是以 vmlinuz 开头,后面跟着版本信息,这些就是内核.我们要 ...
- syscolumns表中所有字段的意思
--syscolumns表中所有字段的意思 name sysname --列名或过程参数的名称. id int --该列所属的表对象 ID,或与该参数关联的存储过程 ID. xtype tinyint ...
- Android判断网络状态
package com.ch.services; import com.ch.utils.NetWorkUtils; import android.app.Service; import androi ...
- halcon学习笔记——机器视觉工程应用的开发思路【转】
转自:http://www.cnblogs.com/hanzhaoxin/archive/2013/02/15/2912879.html 机器视觉工程应用主要可划分为硬件和软件两大部分. 硬件:工程应 ...
- cpu进程调度---RT Throttling【转】
转自:http://book.2cto.com/201302/16291.html RT Throttling是对分配给实时进程的CPU时间进行限制的功能.使用实时调度策略的进程由于bug等出现不可控 ...
- oracle日期函数2!
1.日期时间间隔操作 当前时间减去7分钟的时间 select sysdate,sysdate - interval '7' MINUTE from dual 当前时间减去7小时的时间 ...
- iOS 开发 证书总结 开发证书和生产证书的区别
IOS开发 证书总结 开发者证书 ------>> 开发证书是你在真机推送时 用得, 生产证书是app 上架之后 推送给用户用的 首先你必须获得apple开发者证书,上图这个文件就是 ...
- Linux中查找最耗性能的JAVA代码
在这里总结一下查找Linux.Java环境下最耗CPU性能的代码段的方法.基本上原理就是使用top命令查看最耗cpu的进程和线程(子进程).使用jstack把java线程堆栈给dump下来.然后,在堆 ...
- 几个常见的Laravel报错及解决方法
报错:「Can't swap PDO instance while within transaction」 transactions >= 1) {throw new RuntimeExcept ...
- PHP将多张小图拼接成一张大图
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...