Решение
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
private static final String SERVER_URL = "<http://127.0.0.1:7777/>";
// Helper function to perform the MEW request
private static List<String> ask(String... variableNames) throws IOException {
HttpURLConnection connection = null;
try {
URL url = new URL(SERVER_URL);
connection = (HttpURLConnection) url.openConnection();
// Set the custom MEW method
try {
// Standard way, might throw ProtocolException for non-standard methods
// on some older Java versions or configurations.
connection.setRequestMethod("MEW");
} catch (ProtocolException pe) {
// Fallback using reflection if standard setRequestMethod fails
// This might be necessary in restricted environments or older JVMs
try {
java.lang.reflect.Field methodField = HttpURLConnection.class.getDeclaredField("method");
methodField.setAccessible(true);
methodField.set(connection, "MEW");
} catch (NoSuchFieldException | IllegalAccessException e) {
// If reflection also fails, rethrow the original exception or a custom one
throw new IOException("Failed to set MEW method using standard or reflection.", pe);
}
}
connection.setDoOutput(false); // MEW requests typically don't have a body
connection.setDoInput(true);
// Set the X-Cat-Variable header
// Using String.join handles the case of single vs multiple variables cleanly
String headerValue = String.join(", ", variableNames);
connection.setRequestProperty("X-Cat-Variable", headerValue);
// Connect and check response code (optional but good practice)
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
// Handle potential errors like 404 if needed, although
// the problem description implies success if rules are followed.
// For this problem, we might assume 200 OK if requests are valid.
// System.err.println("Error: Received response code " + responseCode);
// You might throw an exception or return an empty list depending on requirements
// For simplicity here, we proceed assuming 200 OK for valid logic.
}
// Read the X-Cat-Value headers
List<String> values = new ArrayList<>();
Map<String, List<String>> headers = connection.getHeaderFields();
// Iterate through headers, checking for X-Cat-Value case-insensitively
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
if (entry.getKey() != null && entry.getKey().equalsIgnoreCase("X-Cat-Value")) {
// Add all values associated with this header key
// (Handles cases where the server sends multiple headers with the same name)
values.addAll(entry.getValue());
}
}
// Sort the values lexicographically as specified
Collections.sort(values);
return values;
} finally {
// Disconnect connection in the finally block
if (connection != null) {
connection.disconnect();
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
// Read the four variable names
String a = reader.readLine();
String b = reader.readLine();
String c = reader.readLine();
String d = reader.readLine();
String aa = "";
String bb = "";
String cc = "";
String dd = "";
// Request 1: Get values for a and b
List<String> q1 = ask(a, b); // Returns sorted [value(a), value(b)] or [value(b), value(a)]
// Request 2: Get values for b and c
List<String> q2 = ask(b, c); // Returns sorted [value(b), value(c)] or [value(c), value(b)]
// Logic based on comparing the results of the first two requests
if (q1.equals(q2)) {
// This implies value(a) == value(c).
// The common value is value(a) == value(c).
// The other value in q1 (and q2) must be value(b).
// Request 3: Get values for a, c, d
// Since value(a) == value(c), the response will contain value(a), value(a), value(d) sorted.
List<String> q3 = ask(a, c, d); // Returns sorted [valA, valA, valD]
// Check which pair is equal to determine value(a)/value(c)
if (q3.get(0).equals(q3.get(1))) { // Values are [valA, valA, valD]
aa = q3.get(0); // valA
cc = q3.get(1); // valA
dd = q3.get(2); // valD
} else { // Values must be [valD, valA, valA] because q3 is sorted
dd = q3.get(0); // valD
aa = q3.get(1); // valA
cc = q3.get(2); // valA
}
// Determine value(b). It's the value in q1 that is NOT value(a).
if (aa.equals(q1.get(0))) {
bb = q1.get(1);
} else {
bb = q1.get(0);
}
} else {
// This implies value(a) != value(c).
// The value for 'b' must be the common element between the sorted lists q1 and q2.
if (q1.get(0).equals(q2.get(0))) { // Common value is at index 0 in both
bb = q1.get(0); // value(b)
aa = q1.get(1); // value(a) is the other element in q1
cc = q2.get(1); // value(c) is the other element in q2
} else if (q1.get(0).equals(q2.get(1))) { // Common value is q1[0] and q2[1]
bb = q1.get(0); // value(b)
aa = q1.get(1); // value(a)
cc = q2.get(0); // value(c)
} else if (q1.get(1).equals(q2.get(0))) { // Common value is q1[1] and q2[0]
bb = q1.get(1); // value(b)
aa = q1.get(0); // value(a)
cc = q2.get(1); // value(c)
} else { // Common value must be q1[1] and q2[1]
bb = q1.get(1); // value(b)
aa = q1.get(0); // value(a)
cc = q2.get(0); // value(c)
}
// Request 3: Get value for d
List<String> q3 = ask(d); // Returns [value(d)]
dd = q3.get(0);
}
// Write the results
writer.write(aa);
writer.newLine();
writer.write(bb);
writer.newLine();
writer.write(cc);
writer.newLine();
writer.write(dd);
writer.newLine();
// Close streams
reader.close();
writer.close();
}
}