一、题目
试题编号: | 201703-3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
试题名称: | Markdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
时间限制: | 1.0s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
内存限制: | 256.0MB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
问题描述: | 问题描述 Markdown 是一种很流行的轻量级标记语言(lightweight markup language),广泛用于撰写带格式的文档。例如以下这段文本就是用 Markdown 的语法写成的: 输入格式 输入由若干行组成,表示一个用本题规定的 Markdown 语法撰写的文档。 输出格式 输出由若干行组成,表示输入的 Markdown 文档转换成产生的 HTML 代码。 样例输入 # Hello 样例输出 <h1>Hello</h1> 评测用例规模与约定 本题的测试点满足以下条件:●本题每个测试点的输入数据所包含的行数都不超过100,每行字符的个数(包括行末换行符)都不超过100。 ●除了换行符之外,所有字符都是 ASCII 码 32 至 126 的可打印字符。 ●每行行首和行末都不会出现空格字符。 ●输入数据除了 Markdown 语法所需,内容中不会出现 `#`、`*`、`_`、`[`、`]`、`(`、`)`、`<`、`>`、`&` 这些字符。 ●所有测试点均符合题目所规定的 Markdown 语法,你的程序不需要考虑语法错误的情况。 每个测试点包含的语法规则如下表所示,其中“√”表示包含,“×”表示不包含。
提示 由于本题要将输入数据当做一个文本文件来处理,要逐行读取直到文件结束,C/C++、Java 语言的用户可以参考以下代码片段来读取输入内容。 |
二、思路
大模拟,题目要求比较多,每一个规则单独写一个函数,然后在进行调用。
三、代码
先贴出自己写的代码,只有30分,只写了几个比较简单的规则
import java.util.*;
public class Main{
static String out="";
static boolean section=false;
static boolean ul=false;
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String line="";
while(in.hasNextLine()){
line=in.nextLine();
if(line.equals("")){
if(section)
section=false;
if(ul)
ul=false;
}else{
if(line.charAt(0)=='#'){
out+=title(line);
}else if(line.charAt(0)=='*'){
out+=uderList(line);
}else{
out+=Sec(line);
}
}
}
in.close();
System.out.println(out);
}
private static String Sec(String line) {
String str="";
str+=section?"":"<p>";
if(!section) section=true;
else out=out.substring(0, out.length()-5)+"\n";
line=empe(line);
line=link(line);
str+=line+"</p>";
return str+"\n";
}
private static String uderList(String line) {
String res="";
res+=ul?"":"<ul>\n";
if(!ul) ul=true;
else {
out=out.substring(0, out.length()-7);
System.out.println(out);
}
line=line.substring(1, line.length());
line=removeBlank(line);
line=empe(line);
line=link(line);
res+="<li>"+line+"</li>\n</ul>";
return res+"\n";
}
private static String title(String line) {
String res="";
int count=0;
for(int i=0;i<line.length();i++){
if(line.charAt(i)=='#')
count++;
else break;
}
res=line.replaceAll("#", "");
// System.out.println(res);
res=removeBlank(res);
res=empe(res);
res=link(res);
return "<h"+count+">"+res+"</h"+count+">\n";
}
private static String link(String res) {
String str="";
for(int i=0;i<res.length();i++){
char ch=res.charAt(i);
if(ch=='['){
int index=res.substring(i+1, res.length()).indexOf(")");
str+=tranform(str.substring(i+1,index));
i=index;
}else
str+=ch;
}
return str;
}
private static String tranform(String res) {
String str="";
int index=res.indexOf("]");
String href=res.substring(0, index-1);
String lin=res.substring(index+2,res.length());
return "<a href=\""+lin+"\">"+href+"</a>";
}
private static String empe(String res) {
String str="";
boolean is=false;
for(int i=0;i<res.length();i++){
if(res.charAt(i)=='_'){
str+=is?"</em>":"<em>";
is=!is;
}else
str+=res.charAt(i);
}
return str;
}
private static String removeBlank(String line) {
for(int i=0;i<line.length();i++){
if(!line.substring(i,i+1).equals(" "))
return line.substring(i,line.length());
}
return "";
}
}
下面是别人的满分代码
import java.util.*;
public class Main {
String out = ""; // 转换结果
boolean section = false; // 段落标识
boolean ul = false; // 无序列表标识
public static void main(String[] args) {
new Main().run();
}
public void run(){
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String line = sc.nextLine(); // 读取一行
// 逐行读取,按各区块的格式处理
if(!line.equals("")){ // 删除空行
char ch=line.charAt(0);
switch(ch){
case '*': // 无序列表
String s1 = unorderList(line);
out += s1;
break;
case '#': // 标题
out += title(line);
break;
default: // 段落
String s2 = sections(line);
out += s2;
break;
}
}else{ // 有空行,区块结束
if(section){ // 段落区块,修改标识
section = false;
}
if(ul){ // 无序列表区块,修改标识
ul = false;
}
}
}
sc.close();
// 输出结果
System.out.print(out);
}
// 强调处理
public String emphasis(String str){
String res = "";
// 将'_'转为<em>标签
boolean tail = false; // 区分标签的开始和结束
for (int i = 0; i < str.length(); i++) {
String t = str.substring(i, i+1);
if(t.equals("_")){
res += tail ? "</em>" : "<em>";
tail = !tail;
}else{
res += t;
}
}
return res;
}
// 超链接转换
public String transform(String str){
// 划分链接和内容
String s[] = new String[2];
int index = str.indexOf("]");
s[0] = str.substring(1, index);
s[1] = str.substring(index+2, str.length()-1);
return "<a href=\"" + s[1] + "\">" + s[0] + "</a>";
}
// 超链接处理
public String link(String str){
String res = "";
int i = 0;
while(i < str.length()){
String t = str.substring(i, i+1);
if(t.equals("[")){
int index = str.substring(i+1, str.length()).indexOf(")"); // ')'的索引
res += transform(str.substring(i, i+index+2));
i += index+1; // 继续搜索
}else{
res += t;
}
i++;
}
return res;
}
// 处理无序列表
public String unorderList(String str){
String res = "";
// 是否添加<ul>标签
res += ul ? "" : "<ul>\n";
// 修改标识或结束符
if(!ul) ul = true;
else out = out.substring(0, out.length()-7) + "\n";
// 去除'*'和空格
str = str.substring(1, str.length());
str = removeBlank(str);
// 行内处理
str = emphasis(str);
str = link(str);
// 添加内容
res += "<li>" + str + "</li>\n</ul>";
return res + "\n";
}
// 处理段落
public String sections(String str){
String res = "";
// 是否添加<p>标签
res += section ? "" : "<p>";
// 修改标识或结束符
if(!section) section = true;
else out = out.substring(0, out.length()-5) + "\n";
// 行内处理
str = emphasis(str);
str = link(str);
// 紧接之前的行进行追加
res += str + "</p>";
return res + "\n";
}
// 处理标题区块
public String title(String str){
// 统计'#'的个数
int count = 0;
for (int i = 0; i < str.length(); i++) {
if(!str.substring(i, i+1).equals("#")){
count = i;
break;
}
}
// 截取标题的内容(去除所有的'#'和空格)
str = str.replaceAll("#", "");
str = removeBlank(str);
// 行内处理
str = emphasis(str);
str = link(str);
return "<h"+count+">" + str + "</h"+count+">\n";
}
// 去除空格,避免把内容中的空格出去,所以不用string.replaceAll(" ","");方法
public String removeBlank(String str){
for (int i = 0; i < str.length(); i++) {
if(!str.substring(i, i+1).equals(" ")){
return str.substring(i, str.length());
}
}
return "";
}
}