package com.javarush.task.task19.task1918;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/*
Знакомство с тегами
Info about Leela Turanga Leela
Supergirl
Turanga Leela
Turanga Leela
Super
girl
Шаблон тега:
<tag>text1</tag>
<tag text2>text1</tag>
<tag
text2>text1</tag>
*/
public class Solution
{
public static void main(String[] args) throws IOException
{
BufferedReader consol = new BufferedReader(new InputStreamReader(System.in));
BufferedReader file = new BufferedReader(new FileReader(consol.readLine()));
StringBuilder text = new StringBuilder();
while (file.ready())
{
text.append(consol.readLine());
}
consol.close();
file.close();
List<Integer> starts = new ArrayList<>();
List<Integer> ends = new ArrayList<>();
int indexStart = 0;
int indexEnd = 0;
String tag = args[0];
String startTag = "<" + tag;
String endTag = "</" + tag + ">";
while (indexEnd < text.length() - 1)
{
indexStart = text.indexOf(startTag, indexStart);
starts.add(indexStart);
indexStart = indexStart + startTag.length();
indexEnd = text.indexOf(endTag, indexEnd);
ends.add(indexEnd);
indexEnd = indexEnd + endTag.length();
}
List<Coordinates> coordinates = new ArrayList<>();
for (int i = 0; i < ends.size(); i++)
{
for (int j = starts.size() - 1; j >= 0; j--)
{
if (starts.get(j) != null && ends.get(i) > starts.get(j))
{
coordinates.add(new Coordinates(starts.get(j), ends.get(i)));
starts.set(j,null);
break;
}
}
}
coordinates.sort(new Sorted());
for (Coordinates coordinate : coordinates)
{
String command = null;
command = text.substring(coordinate.getStart(), coordinate.getEnd() + endTag.length());
System.out.println(command);
}
}
public static class Sorted implements Comparator<Coordinates>
{
@Override
public int compare(Coordinates o1, Coordinates o2)
{
if (o1.getStart() < o2.getStart())
return -1;
else if (o1.getStart() > o2.getStart())
return 1;
else return 0;
}
}
public static class Coordinates
{
private Integer start;
private Integer end;
public Coordinates(Integer start, Integer end)
{
this.start = start;
this.end = end;
}
public int getStart()
{
return start;
}
public int getEnd()
{
return end;
}
}
}
Нелли
38 уровень
Почему не проходит ни 1 проверку
Обсуждается
Комментарии (3)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
hidden #3303140
23 декабря 2024, 17:53
А если в тексте будет тег <tagx>, что будет с результатом?
*Дальше не читал код
0
Нелли
24 декабря 2024, 13:32
В ответе ставят такие же ограничения:
String openedTag = "<" + tag;
String closedTag = "</" + tag;
Я так понимаю в задаче не предполагается вариант <tagx>
0
Нелли
24 декабря 2024, 13:35
И даже если бы проблема была в этом. Все равно не понятно почему по пунктам 1,2,3,4 требований не проходит. В данном случае только п.5 будет выдавать ошибки тестирования.
![]()
0