The solution proceeds in two steps. The first step is to read and store pairs of strings, and the second step is to perform a series of lookups. The most effective way to store the media types is using an associative array (such as the STL map in C++, or HashMap in Java), however the list is small enough that an array of strings would suffice. Determining the file extension requires first finding the position of the final ‘.’ character (using strrchr or rfind in C++, or lastIndexOf in Java), then discarding the remainder of the string (using substr in C++, or substring in Java).
just addition: n lines entered two strings, 1st is extension, 2nd is file type. and then q lines user inputs media names. coders task is to check if there exist any extension at any given file name, if no coder outputs “unknown”, if yes, coder now seeks for same extension’s file type through saved file extensions and types char array. if coder finds any equivalent file type for existing extension, then he outputs corresponding file type, if vice versa, he then outputs “unknown”
Hi guys! I’m very new to this website as well as competitive programming, so my code is probably not optimal. However, I’m just wondering why it doesn’t work. I tested the test case myself, and it was fine; the logic also seems fine, but the websites says that it is incorrect (not an error).
import java.io.;
import java.util.;
public class Main {
public static void main(String[] args) throws IOException {
HashMap<String, String> hm = new HashMap<String, String>();
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String str = f.readLine();
String[] splitstr = str.split(" ");
int N = Integer.parseInt(splitstr[0]);
int Q = Integer.parseInt(splitstr[1]);
for (int i = 0; i < N; i++) {
String media = f.readLine();
int spaceIdx = media.indexOf(' ');
hm.put(media.substring(0, spaceIdx), media.substring(spaceIdx + 1));
// out.println(hm);
}
for (int i = 0; i< Q; i++) {
String file = f.readLine();
int lastIdx = file.lastIndexOf(".");