update
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdio>
#include <map>
using namespace std;
string trim(string s){
if (s.empty())
return s;
s.erase(0, s.find_first_not_of(' '));
s.erase(s.find_last_not_of(' ')+1);
return s;
}
void blockdisplay(string s){
//find the []
bool isfirst = true;
int i = 0;
while (i<s.size()){
if (s[i] == '['){
i++;
// until find ]
string name;
while (s[i]!=']'){
name += s[i];
i++;
}
i++;
// find the '('
while (s[i]!='(') i++; // eat the whiter space
i++;
string url;
while (s[i]!=')'){
url += s[i];
i++;
}
i++;
cout<<"<a href=\""+trim(url)+"\">";
blockdisplay(trim(name));
cout<<"</a>";
} else{
if (s[i] == '_'&&isfirst){
isfirst = false;
cout<<"<em>";
} else if (s[i] == '_'&&!isfirst){
isfirst = true;
cout<<"</em>";
} else{
cout<<s[i];
}
i++;
}
}
}
int main(){
freopen("../in.txt", "r", stdin);
string temp;
bool isblock = true;
string html;
while (!cin.eof()){
getline(cin, temp);
if (temp == ""){
continue;
} else if (temp[0] == '#'){
// find the number of '#'
int i;
for (i = 0; i < temp.size(); ++i) {
if (temp[i]!='#') break;
}
cout<<"<h"<<i<<">";
blockdisplay(trim(temp.substr(i)));
cout<<"</h"<<i<<">"<<endl;
} else if (temp[0] == '*'){
cout<<"<ul>"<<endl;
cout<<"<li>";
blockdisplay(trim(temp.substr(1)));
cout<<"</li>"<<endl;
while (getline(cin, temp)&&temp!=""){
cout<<"<li>";
blockdisplay(trim(temp.substr(1)));
cout<<"</li>"<<endl;
}
cout<<"</ul>"<<endl;
} else{
//substitution
cout<<"<p>";
blockdisplay(temp);
while (getline(cin, temp)&&temp!=""){
cout<<endl;
blockdisplay(temp);;
}
cout<<"</p>"<<endl;
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args){
new Main().run();
}
public void run() {
Scanner input = new Scanner(System.in);
StringBuffer paragraph = new StringBuffer();
StringBuffer unorderlist = new StringBuffer();
String line;
while (input.hasNextLine()) {
line = input.nextLine();
if (line.equals("")) {
if (paragraph.length() != 0) {
dealparagraph(paragraph.toString());
paragraph.delete(0, paragraph.length());
} else if (unorderlist.length() != 0) {
dealunorderlist(unorderlist.toString());
unorderlist.delete(0, unorderlist.length());
}
continue;
}
if (line.charAt(0) == '#') {
if (line.contains("_")){
line=dealwithem(line);
}
if (line.contains("[")) {
line = dealwithlink(line);
}
dealtitle(line);
} else if (line.charAt(0) == '*') {
if (line.contains("_")){
line=dealwithem(line);
}
if (line.contains("[")) {
line = dealwithlink(line);
}
unorderlist.append("<li>" + line.substring(1, line.length()).trim() + "</li>\n");
} else {
if (line.contains("_")){
line=dealwithem(line);
}
if (line.contains("[")) {
line = dealwithlink(line);
}
paragraph.append(line + "\n");
}
}
if (paragraph.length() != 0) dealparagraph(paragraph.toString());
else if (unorderlist.length() != 0) dealunorderlist(unorderlist.toString());
}
private String dealwithem(String line) {
int start;
String text;
while ((start = line.indexOf("_")) != -1) {
int end = line.indexOf("_",start+1);
text = line.substring(start + 1, end);
line = line.replace("_" + text + "_", "<em>"+text+"</em>");
}
return line;
}
private String dealwithlink(String line) {
int start;
String text, link;
while ((start = line.indexOf("[")) != -1) {
int end = line.indexOf("]");
text = line.substring(start + 1, end);
int parentheses1 = line.indexOf("(");
int parentheses2 = line.indexOf(")");
link = line.substring(parentheses1 + 1, parentheses2);
line = line.replace("[" + text + "](" + link + ")", "<a href=\"" + link + "\"" + ">" + text + "</a>");
}
return line;
}
private void dealunorderlist(String s) {
System.out.println("<ul>\n" + s + "</ul>");
}
private void dealparagraph(String line) {
System.out.println("<p>" + line.trim() + "</p>");
}
private void dealtitle(String line) {
int num = line.lastIndexOf("#") + 1;
String content = line.substring(num, line.length());
System.out.println("<h" + num + ">" + content.trim() + "</h" + num + ">");
}
}