1. 程式人生 > >CCF 2017-3 03-Markdown java(100分)

CCF 2017-3 03-Markdown java(100分)

使用正則表示式解題,塊標籤用string.matches直接匹配,行內標籤使用Pattern達到多次出現,多次替換的效果。

程式碼得分滿分,耗時156ms

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static String headerRegex = "(#+) +(.+)";
    private static Pattern headerPattern = Pattern.compile(headerRegex);

    private static String liRegex = "\\* +(.*)";
    private static Pattern liPattern = Pattern.compile(liRegex);

    private static List<String> output = new ArrayList<>();
    private static boolean pStarted = false;
    private static boolean ulStarted = false;

    private static Pattern emPattern = Pattern.compile("_([^_]+)_");
    private static Pattern linkPattern = Pattern.compile("\\[([^\\]]+)\\]\\(([^\\)]+)\\)");

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> lines = new ArrayList<>();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
//            if(line.equals("-1"))
//                break;
            lines.add(line);
        }
        scanner.close();

        Matcher matcher;

        for (String line : lines) {
            if (line.length() == 0) {
                appendPEndTag();
                appendUlEndTag();
            } else if (line.matches(headerRegex)) {
                appendPEndTag();
                appendUlEndTag();
                matcher = headerPattern.matcher(line);
                if (matcher.find()) {
                    int count = matcher.group(1).length();
                    String content = matcher.group(2);
                    output.add(String.format("<h%d>%s</h%d>", count, content, count));
                }
            } else if (line.matches(liRegex)) {
                appendPEndTag();
                matcher = liPattern.matcher(line);
                if(!ulStarted){
                    output.add("<ul>");
                    ulStarted = true;
                }
                if (matcher.find()) {
                    String content = matcher.group(1);
                    output.add(String.format("<li>%s</li>", content));
                }
            } else {
                appendUlEndTag();
                if (pStarted) {
                    output.add(line);
                } else {
                    output.add(String.format("<p>%s", line));
                    pStarted = true;
                }
            }
        }

        appendUlEndTag();
        appendPEndTag();

        for(String out : output){
            matcher = emPattern.matcher(out);
            while (matcher.find()){
                String content = matcher.group(1);
                out = out.replace(matcher.group(), String.format("<em>%s</em>", content));
            }

            matcher = linkPattern.matcher(out);
            while (matcher.find()){
                String text = matcher.group(1);
                String link = matcher.group(2);
                out = out.replace(matcher.group(), String.format("<a href=\"%s\">%s</a>", link, text));
            }

            System.out.println(out);
        }
    }

    private static void appendPEndTag() {
        if (pStarted) {
            pStarted = false;
            int lastIndex = output.size() - 1;
            String content = output.get(lastIndex);
            output.set(lastIndex, String.format("%s</p>", content));
        }
    }

    private static void appendUlEndTag() {
        if (ulStarted) {
            ulStarted = false;
            output.add("</ul>");
        }
    }
}