# hihocoder #1197 : Give My Text Back

hihocoder #1197 : Give My Text Back

标签(空格分隔): hihocoder

题目:

Time Limit:10000ms
Case Time Limit:1000ms
Memory Limit:256MB

Description
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:

  1. Each sentence contains at least one word, begins with a letter and ends with a period.

  2. In a sentence the only capitalized letter is the first letter.

  3. In a sentence the words are separated by a single space or a comma and a space.

  4. 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:

  1. Changing the cases of letters.

  2. Adding spaces between words and punctuations.

Given the messed text, can you help Little Ho restore the original text?

Input

A string containing no more than 8192 English letters (a-zA-Z), spaces, commas, periods and newlines which is the messed text.

Output

The original text.

Sample Input

my Name is Little Hi.
His name IS Little ho , We are friends.

Sample Output

My name is little hi.
His name is little ho, we are friends.

  这道题按道理说并不难,可是还是搞了半天。一来是自己太挫,思考问题不够全面,二也有思维惰性,没有养成多深入考虑再动手,特别是自己多想一些测试。hihocoder没有提示,不像LeetCode会给出你哪个测试样例出了错。所有需要自己构建测试样例来debug。英文也需要加强,这样才可以理解清楚题意…开始我就不明白一句话会不会分成多行,还是一行就是只会一行多句…
  实践证明,只有一行多句的情况,每句的结尾都是“.”。hihocoder对末尾的空格等字符也很严格,在末尾多输出空格会报“Presentation Error”错误。

  主要考虑的情况就是该有空格的地方没有,不该有的地方有很多…下面是我用的一些测试样例:

my Name  is Little   Hi. ami           .i hate      hiho                     ,codr. EEENNN.
His   name IS Little ho  ,  We are   friends.
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.
His   name IS Little ho  ,We are   friends.   weoifj   ,jo.
                                  Jo, f, j l, o,o, l. 

  代码本来可以在char数组中操作,这样更省内存,但是那样情况讨论稍微复杂一些,为了省事,就用了StringBuilder…

代码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sin = new Scanner(System.in);

        while (sin.hasNext()) {
            String tmp = sin.nextLine().trim();
            int pre = 0;
            int i = tmp.indexOf(".", pre);

            while (i != tmp.length()-1 && i != -1) {
                String sente = tmp.substring(pre, i + 1);
                giveTextBack2(sente.trim());
                System.out.print(" ");
                pre = i + 1;
                i = tmp.indexOf(".", pre);
            }
            if (i != -1) {
                String sente = tmp.substring(pre, i + 1);
                giveTextBack2(sente.trim());
                pre = i+1;
                System.out.println();
            }
        }
    }

    private static void giveTextBack2(String sentence) {
        StringBuilder res = new StringBuilder();
        res.append(Character.toUpperCase(sentence.charAt(0)));
        int i = 1;
        boolean in = true;
        while (i < sentence.length()) {
            if ( (sentence.charAt(i) >= 'a'&& sentence.charAt(i) <='z')
                || (sentence.charAt(i) >= 'A'&& sentence.charAt(i) <='Z')) {
                res.append(Character.toLowerCase(sentence.charAt(i)));
                in = true;
            } else if (sentence.charAt(i) == ' ') {
                if (in) {
                    res.append(" ");
                    in = false;
                }
            } else if (sentence.charAt(i) == ',') {
                if (in) {
                    res.append(",");
                    res.append(" ");
                    in = false;
                } else {
                    res.deleteCharAt(res.length()-1);
                    res.append(",");
                    res.append(" ");
                    in = false;
                }
            } else if (sentence.charAt(i) == '.') {
                if (in) {
                    res.append(".");
                    in = false;
                } else {
                    res.deleteCharAt(res.length()-1);
                    res.append(".");
                    in = false;
                }
            }
            i++;
        }
        System.out.print(res.toString().trim());
    }
}

你可能感兴趣的:(hihoCoder)