Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.TreeMap;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- enum Tag {
- タグa, タグb, タグc, タグd, タグe, タグf, タグg, タグh, タグi, タグj, タグk, タグl;
- }
- class TagAndAux implements Comparable<TagAndAux> {
- public final Tag tag;
- public final String aux;
- public TagAndAux(Tag tag, String aux) {
- this.tag = tag;
- this.aux = aux;
- }
- @Override
- public int compareTo(TagAndAux o) {
- if (tag.compareTo(o.tag) != 0)
- return tag.compareTo(o.tag);
- return aux.compareTo(o.aux);
- }
- }
- public class TagSort {
- private static String tagToString(Map<TagAndAux, List<String>> block) {
- StringBuilder sb = new StringBuilder();
- for (Entry<TagAndAux, List<String>> e : block.entrySet())
- for (String ee : e.getValue())
- if (e.getKey().aux.isEmpty())
- sb.append("<" + e.getKey().tag + ">" + ee + "</" + e.getKey().tag + ">\n");
- else
- sb.append("<" + e.getKey().tag + " " + e.getKey().aux + ">" + ee + "</" + e.getKey().tag + ">\n");
- block.clear();
- sb.append("\n");
- return sb.toString();
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- Map<TagAndAux, List<String>> block = new TreeMap<TagAndAux, List<String>>();
- StringBuilder sb = new StringBuilder();
- Pattern re = Pattern.compile("^<(.+?)(?: (.+?))?>(.+?)</\\1>$");
- while (in.hasNext()) {
- String line = in.nextLine();
- if (line.isEmpty()) {
- sb.append(tagToString(block));
- } else {
- Matcher m = re.matcher(line);
- if (m.find()) {
- Tag t = null;
- for (Tag e : Tag.values())
- if (e.toString().equals(m.group(1))) {
- t = e;
- break;
- }
- String aux = m.group(2);
- if (aux == null)
- aux = "";
- TagAndAux ta = new TagAndAux(t, aux);
- if (block.get(ta) == null)
- block.put(ta, new ArrayList<String>());
- block.get(ta).add(m.group(3));
- }
- }
- }
- in.close();
- if (!block.isEmpty()) {
- sb.append(tagToString(block));
- }
- System.out.print(sb);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement