settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Jedi Code-X - Java Task

+10 votes

The Jedi and the Sith warfare is ruthless, and every leak of information could be lethal to the Light side’s revolution, that is why encoded messages were created. You need to write a program that decodes the incoming messages and the names of those that sent them.

On the first line of input you will receive n, an integer. On the next n lines you will be receiving strings of random ASCII characters with random length, which will represent the encoded text.

After that you will receive 2 lines. They will contain patterns. You must extract all names that consist of English alphabet letters, with length equal to the length of the pattern given on the first line, and prefix the given pattern itself. Then you must extract all messages which consist of English alphabet letters and digits, with length equal to the length of the pattern given on the second line, and prefix the given pattern itself. A character which does not follow the content conditions specified above denotes the end of a particular Jedi name / message.

On the last line of input you will get integers separated by a space. Every integer is an index of a message. The first Jedi found in the encoded text corresponds to the first index and the message at that index, the second Jedi to the second index and so on… If a particular message’s index is non-existent you ignore it and assign the next message with a valid index to the current Jedi, if such a message does not exist the Jedi is left with no message. In the situation that a Jedi’s message has an invalid index and he skips through the messages to find a valid one, the Jedi after him (if one exists) will take the message after the one the first Jedi took. If there are more indexes than needed, ignore the extra indexes.

Input:

  • On the first line you will get the integer n.
  • On the next n lines you will get random amounts of random ASCII characters.
  • After that you will get the two patterns each on a new line.
  • On the last line you will get integers separated by a single space.

Output:

  • The output is simple. You must print the Jedi, if the messages are less than the Jedi, print only those Jedi that have messages.
  • Jedi must be printed in the following format: {Jedi name} – {Jedi message}

Constraints:

  • N will be a valid integer in the range [0, 1000].
  • The encoded text will consist of ASCII characters.
  • The prefix patterns can consist of any ASCII character.
  • The integers (indexes of messages) will be valid integers in range [0, 1000].
  • Allowed time / memory: 100ms / 16MB.

Examples:

Input

Output

4

1-02948091jeknm,a sd,nbjwhu3hroLKjahefkljalsj#=#=#pesho

1gjq&&&geo*nhvjsfbsbdkfjq.311/.24/3.4,l2

3u827582929@@@@#=#=#gosho

&&&ped-&&&eraeesmdfjbdfspefkowekf

#=#=#

&&&

2 1

pesho - ped

gosho - geo

Input

Output

5

asdasdasd------Petkan123asdasd sasd

------Goshko-asdasdfddfgasdada 1r23

------Lilqna564!3876876429

*)(@*#)(&$%*^&------Stamat+as

:-@-@-ONIGH u305uvwoenh N{-@-@-OAIHF

------

-@-@-

2 1 1 3

Petkan - OAIHF

Goshko - ONIGH

Lilqna - ONIGH

 

asked in Java category by user richard8502

1 Answer

+2 votes
 
Best answer

Here is the solution:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Pr_03_Jedi_CodeX {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int lineCount = Integer.parseInt(reader.readLine());

        StringBuilder input = new StringBuilder();

        for (int i = 0; i < lineCount; i++) {
            input.append(reader.readLine());
        }

        String namePattern = reader.readLine();
        String nameRegex = String.format("%s(?<name>[a-zA-Z]{%d}(?!([a-zA-Z])))", namePattern, namePattern.length());
        Pattern namePat = Pattern.compile(nameRegex);
        Matcher nameMatcher = namePat.matcher(input.toString());

        String messagePattern = reader.readLine();
        String messageRegex = String.format("%s(?<message>[a-zA-Z0-9]{%d}(?![a-zA-Z0-9]))", messagePattern, messagePattern.length());
        Pattern messagePat = Pattern.compile(messageRegex);
        Matcher messageMatcher = messagePat.matcher(input.toString());

        List<String> jediNames = new ArrayList<>();
        List<String> jediMessages = new ArrayList<>();

        while (nameMatcher.find()) {
            jediNames.add(nameMatcher.group("name"));
        }

        while (messageMatcher.find()) {
            jediMessages.add(messageMatcher.group("message"));
        }

        Integer[] messageIndex = Arrays.stream(reader.readLine().split(" "))
                .map(Integer::parseInt)
                .toArray(Integer[]::new);

        int currentJediIndex = 0;
        for (int i = 0; i < messageIndex.length; i++) {
            if (messageIndex[i] - 1 < jediMessages.size() && currentJediIndex < jediNames.size()) {
                System.out.printf("%s - %s%n", jediNames.get(currentJediIndex), jediMessages.get(messageIndex[i] - 1));
                currentJediIndex++;
            }
        }
    }
}

 

answered by user paulcabalit
selected by user golearnweb
...