We need to parse the problem carefully.
Given T test cases up to 50000. For each test case, we have a string (non-empty, length <= 10, composed of printable ASCII characters 32-126). We need to output:
-
"Error" if the string does not represent a correct pair of cells (i.e., not of format "XY-XY" where X is a letter a-h, Y is digit 1-8, total length 5, third character is dash '-').
-
If it is correctly formatted, then check if it represents a correct knight move: from (source) to (dest) it is a knight's move. Knight moves: moves two squares in one direction and one square perpendicular. In chess, knights move in L-shape: (dx, dy) = (1,2), (2,1), (1,-2), (2,-1), (-1,2), (-2,1), (-1,-2), (-2,-1). So absolute differences: (|dx|,|dy|) = (1,2) or (2,1).
So output "Yes" if it's a valid knight move; "No" otherwise.
Important: The string must be exactly length 5, positions: index 0-1: source cell, index 2: dash '-', index 3-4: destination cell. Also we must ensure that source and destination are valid cells: letter a-h, digit 1-8. Also presumably the source and destination could be same? That would not be a knight move; output "No". Also maybe they could be same, but it's not correct knight move.
Edge cases: The string could have spaces or other characters; length may not be 5; dash missing; extra characters? But length <=10 so they might include extra characters beyond 5? The specification: "The string represents the correct pair of cells on the chess board if it composed of 5 characters where first two characters represent the cell where chess figure was, 3rd character is the dash "-" and the last two characters represent the destination cell." So it must be exactly 5 characters. If it's longer or shorter, it's not correct. However note they say "composed of 5 characters where ...". So I'd interpret that as the string must have exactly length 5 and follow that pattern. So for length !=5, output Error. Also check that characters at indices 0,1,3,4 are valid: index0 letter a-h (lowercase), index1 digit 1-8, index3 letter a-h, index4 digit 1-8. And index2 is '-'. If any fails, Error.
Now also consider that the input string may contain leading/trailing spaces? The input is given per line; reading the whole line as the string. In typical programming contest input reading, we might read the line as is including any spaces. But the problem says: "The only line of each test case contains a non-empty string composed the characters with ASCII-codes from 32 to 126. The length of the string is not greater than 10." So there could be spaces. For example, the sample includes "a3 c4" which likely has a space: "a3 c4"? Actually sample Input:
4
a1-b3
d2-h8
a3 c4
ErrorError
The third test case is "a3 c4". That's length 5? Let's count: 'a','3',' ','c','4'? Actually "a3 c4" is 5 characters: a,3,space,c,4. That's not a dash; also the space is not dash, and the cells maybe not valid? The source "a3" is valid, but the third character is space not dash, so it's not correct format -> Error. The fourth case "ErrorError" is length 10? Actually "ErrorError" is 10 characters? "E","r","r","o","r","E","r","r","o","r"? That's 10. So not length 5, format invalid -> Error. So our logic matches.
So we need to output "Error", "Yes", or "No". Note: "Yes" and "No" only when format is correct.
Constraints T up to 50000, easy O(T).
Implementation: For each test case, read line. Use getline after reading T? Careful about newline. Since T up to 50000, we can just read lines using standard input. In C++ we can read integer T, then use a loop, read each line with getline, but need to consume newline after reading T. Simpler: read T, then for i from 0 to T-1, read a string via cin >> std::string? But cin >> string will stop at whitespace. Since the test string may contain spaces, we need to read the whole line. So we should use getline. So after reading T, call cin.ignore() to discard the newline. Then for each test, use std::getline(cin, s). s may contain trailing newline removed. Ensure we handle empty lines? Problem says non-empty, so each line has at least one character.
Now check length: if s.length() != 5 -> Error.
Else if s[2] != '-' -> Error.
Else if s[0] not in 'a'..'h' or s[1] not in '1'..'8' or s[3] not in 'a'..'h' or s[4] not in '1'..'8' -> Error.
Otherwise compute dx = s[3] - s[0] (as integer difference), dy = s[4] - s[1]; but careful: s[4] is a character digit; we can convert to int: int d1 = s[1] - '0'; int d2 = s[4] - '0'; then diff_x = abs(s[3] - s[0]) (since letters contiguous from 'a' to 'h'), diff_y = abs(d2 - d1). Then check if (diff_x == 1 && diff_y == 2) or (diff_x == 2 && diff_y == 1) -> Yes else No.
Edge case: if source and dest same: diff_x=0,diff_y=0 -> not (1,2) or (2,1) -> No.
Now what about strings that contain extra characters beyond 5 but length <=10? According to specification they are not correct. So we must output Error.
Now some tricky: The source or destination could be something like "i9" which is invalid. So we check exactly letters a-h, digits 1-8.
Implementation: We must be careful about using getline after reading T: typical C++ code:
int T;
cin >> T;
cin.ignore(numeric_limits::max(), '\n');
while (T--) {
string s;
getline(cin, s);
// process
}
But note: the problem may have lines that could contain leading/trailing spaces? Possibly. Using getline will capture all characters up to newline, including spaces. That's correct.
Now T <= 50000, so O(T) is fine.
Now test sample:
a1-b3 -> valid format? Yes. Check knight: a1->b3: diff_x=1 (b-a), diff_y=2 (3-1)=2 -> Yes.
d2-h8 -> diff_x: h-d = 4? Actually letters: d=4? But compute: 'd' to 'h' = 4? Wait 'd' ascii 100, 'h' 104 diff=4; diff_y = 8-2=6; not (1,2) or (2,1) -> No.
a3 c4 -> length 5 but s[2] is space, not dash, so Error.
ErrorError -> length 10, not 5 -> Error.
Matches.
Now confirm some edge cases: Suppose input is "a1-b9". Source a1 valid, dest b9 but digit 9 is invalid (must be 1-8). So Error. Good.
What about "a1-a1"? Format ok, same square, diff 0,0 -> No.
What about "a1-c2"? diff_x = 2 (c-a), diff_y = 1 (2-1) -> Yes.
Alright.
Now some may consider that the dash could be some other character, so we check.
Now note: The problem says: "the string composed of the characters with ASCII-codes from 32 to 126." So it could include anything; we need to treat accordingly.
Now code.
We'll produce code in C++ or Python? The instructions didn't specify language, but typical solutions are C++ for performance. However T=50000 is not huge, Python is fine. But I'll produce a solution in C++.
Now ensure to handle large input quickly: use ios_base::sync_with_stdio(false); cin.tie(nullptr); and use getline.
One nuance: after reading T with cin >> T, the newline remains; if we call cin.ignore() once, we discard that newline. Then subsequent getlines read each line.
But we must be careful: if there are any extra spaces after T on the same line? Usually T is on its own line; reading T with >> consumes the integer and leaves the newline. So ignore.
Now code:
#include
#include
#include // not needed
#include // not needed
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
cin.ignore(); // ignore newline after T
while (T--) {
string s;
getline(cin, s);
if (s.length() != 5) {
cout << "Error\n";
continue;
}
if (s[2] != '-') {
cout << "Error\n";
continue;
}
// Check cells
if (!(s[0] >= 'a' && s[0] <= 'h') || !(s[1] >= '1' && s[1] <= '8') ||
!(s[3] >= 'a' && s[3] <= 'h') || !(s[4] >= '1' && s[4] <= '8')) {
cout << "Error\n";
continue;
}
// Compute differences
int dx = abs(s[3] - s[0]);
int dy = abs((s[4] - '0') - (s[1] - '0'));
if ((dx == 1 && dy == 2) || (dx == 2 && dy == 1)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
That solves.
But we must double-check: Is it guaranteed that letters are lowercase? Yes, the problem says "first character is a lowercase Latin letter from a to h". So enforce that.
Now also consider that maybe the string could have trailing spaces, which would cause length >5? But we get the line with getline, which includes trailing spaces? If the line contains trailing spaces, they would be included in s, making length >5, so we output Error. That's consistent because it wouldn't be exactly "XY-XY". But maybe there is a line with exactly 5 characters but the last character is a space? That would not match cell because digit must be 1-8; space is not digit, so Error. So fine.
Now if the string is exactly "a1-b3" but has leading spaces? That would cause length >5 if spaces at beginning, so Error. That's fine because specification says it must be composed of 5 characters exactly; presumably no spaces.
Thus solution is correct.
Now let's think about potential pitfalls: T up to 50000, reading lines with getline is fine.
Now the output must be exactly "Error", "Yes", "No" with capital letters as shown.
Now what about the case where the dash is missing but string length 5 and cells are valid? e.g., "a1b3" length 4? Actually "a1b3" is length 4? No, "a1b3" length 4? Let's count: a,1,b,3 = 4. Not 5, so error. "a1 b3" length 5 with space: error because s[2] is space. Good.
Thus implementation straightforward.
Now produce final answer.
We should also note that the problem says "the string composed the characters with ASCII-codes from 32 to 126. The length of the string is not greater than 10." So they may include null? But getline will handle.
Now final code.
I'll write the final answer accordingly. ```cpp
#include
#include
#include
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
cin.ignore(); // discard the newline after T
while (T--) {
string s;
getline(cin, s);
// A correct pair must have exactly 5 characters
if (s.length() != 5) {
cout << "Error\n";
continue;
}
// The middle character must be '-'
if (s[2] != '-') {
cout << "Error\n";
continue;
}
// Validate source and destination cells
bool valid = (s[0] >= 'a' && s[0] <= 'h') &&
(s[1] >= '1' && s[1] <= '8') &&
(s[3] >= 'a' && s[3] <= 'h') &&
(s[4] >= '1' && s[4] <= '8');
if (!valid) {
cout << "Error\n";
continue;
}
// Compute coordinate differences
int dx = abs(s[3] - s[0]);
int dy = abs((s[4] - '0') - (s[1] - '0'));
// Check knight move
if ((dx == 1 && dy == 2) || (dx == 2 && dy == 1))
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}