We are given two lists (unordered) of strings, each of size n. Each string is a valid T-shirt size as per the defined pattern: either "M" or from 0 to 3 'X's followed by 'S' or 'L'. So the set of possible strings is: M, S, XS, XXS, XXXS, L, XL, XXL, XXXL. That's 9 possible sizes.
We can modify characters in the strings. In one operation, we can replace a character at any position in any word with any uppercase Latin letter. We cannot remove or add letters; so we can't change length. So the length of each word must remain the same after modifications? Actually we are not allowed to remove or add letters. So if we start with a string of some length, we can only replace characters, so its length stays the same. But we could change it into another string of same length? Possibly changing letters to different ones. So the length of each word is fixed. But we can also reorder words? The lists are unordered, meaning we consider the multiset of strings. So we are allowed to assign each string in list a (previous year) to some string in list b (current) after modifications. Since we can modify each string individually, we can choose which a_i to turn into which b_j, as long as we can transform by character replacements, and we want to minimize total number of character replacements. The catch: modifications are independent; each a_i can be modified to become some b string. The total cost is sum of number of characters changed over the assignments. Because the lists are unordered, we can pair each a string to a b string to minimize total changes. However, we are also allowed to perhaps modify multiple strings; but essentially we need to produce exactly the multiset of b strings. So we need a bijection between the n strings from a and the n strings from b (since both are multisets). Each a string can be turned into some b string by a series of character replacements. Since we cannot change length, an a string can only be turned into a b string of the same length. But can an a string of a certain length be turned into a b string of a different length? No, because replacement does not change length. So we must match strings of same length. However, note that all valid sizes have lengths: "M" length 1; "S" length 1? Actually "S" is valid? Yes, from description: from 0 to 3 "X" followed by "S" or "L". So 0 X's + S = "S". So "S" is length 1. Similarly "L" length 1. So there are three sizes of length 1: M, S, L. They are distinct strings of length 1. For length 2: XS, XL. For length 3: XXS, XXL. For length 4: XXXS, XXXL.
Thus any string a of length L can only be turned into a b string of the same length L, because we cannot add/remove characters. So we have separate groups by length. In each group, we need to match the multiset of a strings (of that length) to the multiset of b strings (of that length) to minimize total number of character changes.
Given that the sizes are limited to these 9 strings, and within each length group, the strings are very similar. In each length, the strings differ only by the last character? Actually for lengths 2,3,4, the strings consist of a sequence of X's and then a final letter S or L. For length 1, strings are M, S, L. So they are single letters.
We need to compute, for each pair of strings (from a and b) of same length, the minimal number of character replacements required to transform one into the other. Since we are allowed to replace any character with any uppercase letter, the minimal number of character differences is simply the Hamming distance (number of positions where they differ) because we can replace exactly those characters. That is the cost. Because we cannot rearrange letters; just replace. So the cost between two strings of same length is the count of positions where they differ.
So the problem reduces to: Given two multisets A and B (each of size n) of strings from a fixed small set, with the constraint that there exists a bijection mapping A to B such that matched strings have same length (implied by existence of some transformations? Actually the problem statement says: It is guaranteed that all sizes in input are valid. It is also guaranteed that Ksenia can produce list b from list a. This guarantee means that there exists some sequence of operations (replacements) that turns the list a into list b, respecting that we cannot add/remove letters. That implies that there is a way to assign each a string to a b string (or perhaps we can modify multiple times and perhaps a string could become multiple b strings? But we need final multiset exactly b, each a string becomes exactly one final string after modifications, so it must match length. So the guarantee ensures that there is a matching based on lengths, i.e., the number of strings of each length in a equals that in b? Possibly not necessarily: because we could modify a string of length L into another of length L? No, length is fixed. So the counts of each length must match. Indeed, to be able to produce b, we need the multiset of lengths to match between a and b. Let's check: In example 2: a: XXXL (length 4) and XXL (length 3). b: XXL (length 3) and XXXS (length 4). So length counts: length3: one in a, one in b; length4: one in a, one in b. So they match. Example 1: a: XS, XS, M (lengths: 2,2,1). b: XL, S, XS (lengths: 2,1,2). So lengths: length1: a has 1 (M), b has 1 (S). length2: a has 2 (XS,XS), b has 2 (XL, XS). So counts match. So indeed the guarantee implies that for each length, the count of strings is the same. So we can treat each length separately.
So the problem is: For each length group (1,2,3,4), we have multisets of strings of that length. We want to match each a string to a b string of same length to minimize total Hamming distance. Because each group is independent and we sum costs. Since the strings are from a small set, we can compute cost matrix and then find minimum cost matching. But n ≤ 100, and the number of types per length is at most 3 (for length 1: M, S, L; for length 2: XS, XL; length 3: XXS, XXL; length 4: XXXS, XXXL). So each group will have at most n strings but types are limited. The matching can be solved via min-cost flow or simply by greedy? But careful: We need to minimize total number of differing characters. Since the strings within a length differ only at certain positions, the cost between pairs is either 0,1, or maybe 2? Let's compute Hamming distances:
Length 1:
- M vs S: differ? M vs S -> different, so cost 1.
- M vs L: cost 1.
- S vs L: cost 1.
Also M vs M:0, etc.
So all pairs have cost 0 or 1.
Length 2: strings are XS and XL. They differ at second character? Actually positions: index0='X', index1='S' or 'L'. So cost = 0 if same, 1 if different (since first character always X, second differs). So cost = 0 for same, 1 for different.
Length 3: XXS and XXL. First two characters are 'X','X' for both; third character S or L. So cost = 0 if same, 1 if different.
Length 4: XXXS and XXXL. Similarly cost = 0 or 1.
Thus in each group, the cost between any two strings of the same type is 0, and between different types it's 1 (since they only differ at the last character). Actually check: For length 1, strings are M, S, L. They all differ from each other, so cost=1 between any distinct pair. So yes, same pattern: within a group, there are types; same type cost 0; different types cost 1.
Therefore the matching problem per group reduces to: we have counts of each type in A and B. We need to assign each A item to a B item; each assignment of different types costs 1, same type costs 0. Minimizing total cost is equivalent to maximizing number of matches of same type. So the minimal total cost = total number of items in that group minus maximum number of matches where types are same. Since each item must be assigned, the minimal number of mismatches is the difference between total count and the maximum possible number of same-type pairings. The maximum number of same-type pairings is simply the sum over types of min(count_A[type], count_B[type]) because we can pair up to the minimum for each type. The rest must be mismatched. Since mismatches cost 1 each, total cost per group = (total count) - sum_{type} min(count_A[type], count_B[type]).
But is there any scenario where a mismatch could cost more than 1? No, because all strings in same length group differ by at most 1 character, as we argued. However, for length 1, what about converting M to S vs M to L? Both cost 1. So yes, any mismatch costs exactly 1.
Thus the total minimal seconds = sum over all lengths L of (count_L - sum over types in that length of min(count_A_type, count_B_type)). But careful: Since groups are independent, we can compute per length. The overall answer = total number of strings (n) minus total number of matches (i.e., sum over all types (over all lengths) of min(count_A[type], count_B[type])). Because the sum over lengths of count_L = n. And the sum over lengths of sum_type min = total matches across all types.
But is it always optimal to match same types as much as possible? Since mismatches cost 1, and there is no additional cost beyond 1, it's always beneficial to match as many identical strings as possible. However, could there be a scenario where matching different types might allow more same-type matches later? This is a classic assignment with cost 0 for identical, 1 for different. The minimum cost is indeed total count - maximum matching of identical. Since each type can only be matched to itself for 0 cost, the maximum 0-cost assignments is limited by the minimum of each type. Any leftover items from A or B must be matched across types, costing 1 each. Since each such cross-type match uses one from A and one from B, and the cost per cross-type is 1, the total cost is max(total A leftovers, total B leftovers). But because total A count per length equals total B count per length, the leftovers are equal: sum (count_A_type - min) = total A - sum min = total B - sum min, which are equal. So cost = (total - sum min). That's the number of mismatched pairs.
Thus answer = n - sum_{t} min(cA[t], cB[t]) where t runs over all valid sizes (9 types). But careful: Is there any possibility to reduce cost further by converting a string of length 2 to a string of length 1 via modifications? No, because we cannot change length. So we must match within length groups. However, our per-type matching across all types automatically respects length because types of different lengths are distinct and we wouldn't match them because min(cA[t], cB[t]) for t in different length groups will be zero unless both have that type, but they are different types anyway. So it's fine.
But is there any more subtlety: Could we transform an XS (length2) into an S (length1) by replacing characters? No, because length must stay same. So it's impossible. So the guarantee that transformation exists implies that the counts of each length match, so we can safely match within each length group.
Thus the answer is simply: compute frequency counts for all valid size strings from the first list, and from the second list. Then total answer = sum over all size strings of |freq_a[size] - freq_b[size]|? Wait, that might not be correct. Because the total cost we derived is n - sum min(f_a, f_b). And note that sum over sizes of |f_a - f_b| = (n - sum min) * 2? Let's see: sum |f_a - f_b| = sum (max - min) = (sum max + sum min - 2 sum min?) Actually: For each type, let f_a, f_b. Then |f_a - f_b| = f_a + f_b - 2min(f_a, f_b). Summing over types: sum |f_a - f_b| = (sum f_a + sum f_b) - 2 sum min = 2n - 2 sum min = 2(n - sum min). So n - sum min = (sum |f_a - f_b|)/2. So answer equals half of sum of absolute differences. That is also intuitive: each mismatch pair contributes 1 to total cost and corresponds to one excess of a type on one side and one deficit on another. So we could also compute answer = (sum_{size} |freq_a[size] - freq_b[size]|)/2.
But need to ensure that we only consider valid sizes? Input only contains valid sizes, so counts for all 9 types; others are zero.
Thus solution is straightforward: read n, read first list of n strings, compute frequency for each size; read second list, compute frequency; then answer = (sum over sizes of abs(freq1[size] - freq2[size])) / 2. Or equivalently, n - sum min(freq1[size], freq2[size]).
Check examples:
Example1:
n=3
a: XS, XS, M
b: XL, S, XS
Counts:
M: a=1, b=0 -> diff=1
S: a=0, b=1 -> diff=1
L: a=0, b=0 -> diff=0
XS: a=2, b=1 -> diff=1
XL: a=0, b=1 -> diff=1
XXS,XXL,XXXS,XXXL: 0 diff.
Sum diffs = 1+1+1+1 = 4; half = 2. matches output.
Example2:
a: XXXL, XXL
b: XXL, XXXS
Counts:
XXL: a=1, b=1 -> diff=0
XXXL: a=1, b=0 -> diff=1
XXXS: a=0, b=1 -> diff=1
others 0. Sum diffs=2; half=1. matches output.
Example3:
a: M, XS
b: XS, M
Counts: M:1,1 diff0; XS:1,1 diff0; sum=0 -> output 0. matches.
So answer is indeed half sum of absolute differences.
But wait: Are there any cases where a mismatch could cost more than 1? Let's double-check length 1 strings: M, S, L. They are all single letters, so Hamming distance between any two distinct is 1. Yes.
But could there be a case where we transform a string like "XS" to "S"? That would require changing length, not allowed. So not possible.
Thus the problem is essentially: given two multisets of strings from a limited set, where each transformation cost equals the number of differing characters (0 if identical, 1 if different but same length, impossible if different length), and it's guaranteed that there exists a transformation (so lengths match), minimal total replacements = number of positions changed across all strings. Since the only possible costs are 0 or 1 per string (if same length) when matching, the minimal number of changes overall equals the number of strings that need to be changed at all? Actually each string may need to be changed in at most one character because any two strings of same length differ in at most one position (they only differ at last character). But could there be a transformation that changes more than one character in a single string? Possibly if we transform an XS (X S) into XL (X L) it's one character change; if we transform XS into S (length mismatch) not possible; if we transform M into S, one change; M into L, one change. So each string that is assigned to a different type requires exactly 1 character change. Strings assigned to same type require 0 changes. So total changes = number of strings assigned to a different type. And we can assign to minimize that number, which is n - maximum matches of identical types.
Thus answer computed as above.
Edge cases: n up to 100, trivial.
Thus solution: Read n, then read first n strings into list A, second n into list B. Compute dictionaries: from valid sizes? Actually we can just count all strings as they appear. There's no need to predefine the set because all inputs are valid. We'll compute freq1 and freq2 as Counter objects. Then answer = sum( abs(freq1[s] - freq2[s]) for s in set(freq1.keys()) union set(freq2.keys()) ) // 2.
Alternatively, answer = n - sum( min(freq1[s], freq2[s]) for s in set ... ).
We'll implement.
Complexity O(n).
But we need to confirm that the guarantee indeed ensures that the sum of diffs is even. Should be because total counts are equal. So integer division by 2 is safe.
Thus final answer.
But wait, there's a nuance: The problem states that Ksenia can't remove or add letters, but she can replace characters arbitrarily. She can also change a string multiple times? But we consider final transformation directly; number of replacements equals Hamming distance, because we can change each differing character once. So that's fine.
Double-check: Suppose we have a string "XS" and we want to turn it into "XXL"? That's not possible because lengths differ (2 vs 3). So not allowed. So matching must be within same length.
Now, is it ever beneficial to not directly match an a string to its final b string, but to first change it to an intermediate string and then reassign? For example, we have a1 and a2, b1 and b2. The total cost is sum of changes for each a_i assigned to its final b_j. Could there be a scenario where swapping assignments leads to lower total number of character changes than the minimum matching? But we are already minimizing over assignments. So that's fine. Since we are allowed to arbitrarily change characters, we can directly change a_i to b_j, no need for intermediate.
Thus solution stands.
Let's also test some custom cases:
Case: n=2, a: M, S; b: M, L. Counts: M:1,1; S:1,0; L:0,1. Diffs: |1-1|=0, |1-0|=1, |0-1|=1 sum=2, half=1. So answer 1. Indeed we can change S to L (1 change). Or change S to M? Actually M already taken, but we can assign: a M -> b M (0 changes), a S -> b L (1 change) total 1. Could there be a better assignment? a S -> b M (1 change), a M -> b L (1 change) total 2, so 1 is minimal. So correct.
Case: n=3, a: M, S, L; b: M, S, L -> diffs zero -> 0.
Case: n=4, a: XS, XS, XL, XL; b: XS, XS, XS, XL. Counts: XS a=2 b=3 diff=1; XL a=2 b=1 diff=1; sum diff=2 -> ans=1. Indeed we need to change one XL to XS (1 change) or one XS to XL? Actually we have two XL in a, one XL in b, so one XL must become XS. That's 1 change. So correct.
Case: mixing lengths: a: M, XS; b: S, XL. But is this possible? Check lengths: a lengths:1,2; b lengths:1,2. So yes, possible. Counts: M:1,0 diff1; S:0,1 diff1; XS:1,0 diff1; XL:0,1 diff1; sum diff=4 half=2. So answer 2. Indeed we need to change M to S (1 change) and XS to XL (1 change). Total 2.
So answer formula works.
Thus we can code.
But wait, there's a subtlety: The problem says "Ksenia can't remove or add letters in any of the words." So each word's length remains constant. That matches our reasoning.
Now, is there any possibility that we can change a string to a different string with same length but more than one character difference? Actually given the structure of sizes, all strings of same length differ only at the last character, so Hamming distance is either 0 or 1. But what if we had "XS" and "XX"? That's not a valid size. So not relevant.
Thus answer simple.
But the problem could be more complex if they allowed arbitrary strings; but they restrict to valid sizes. So indeed it's a simple combinatorial min changes.
Therefore final solution:
Algorithm:
- Read n.
- Initialize two dictionaries (or Counter) freq1 and freq2.
- For i in 1..n: read string s; increment freq1[s].
- For i in 1..n: read string s; increment freq2[s].
- total = 0
For each key in set of keys from both dicts:
total += abs(freq1.get(key,0) - freq2.get(key,0))
ans = total // 2
- Print ans.
Alternatively, we can compute ans = n - sum(min(freq1.get(k,0), freq2.get(k,0)) for k in set).
I'll implement using Counter from collections.
Edge: Input lines might have spaces? Each size is a string without spaces.
Complexities O(n). Works for n up to 100.
Let's write in Python.
Potential pitfalls: reading input, newline handling. Use input().strip().
I'll provide solution.
But wait, the problem statement includes note: "The valid sizes of T-shirts are either "M" or from 0 to 3 "X" followed by "S" or "L"." So "S" and "L" with 0 X are valid. So we have to count them.
Ok.
Now I'll produce final answer.
But also we need to ensure that the guarantee holds: Ksenia can produce list b from a. That ensures lengths match. Our solution would output a number, but if they gave an input where lengths don't match, the answer might be half of odd? But they guarantee it's possible, so fine.
Now I'll produce final answer with explanation.
However, I'll also consider if there might be a scenario where the minimal number of character changes is not simply the number of mismatched pairs because maybe we could change a string to another and then change it again to something else to reduce overall changes? That doesn't make sense because each string ends as a specific b string; we can directly change it to that b string in one go, replacing exactly the differing characters. There's no benefit to intermediate modifications; they would only increase count of changes. So direct transformation is optimal.
Thus solution is correct.
I'll now produce final answer. The problem can be simplified by observing that the valid T-shirt sizes form a small set of nine strings:
M, S, L, XS, XL, XXS, XXL, XXXS, XXXL.
All strings of the same length differ only in the last character (or are identical), so transforming one size into another of the same length requires changing 0 characters if they are the same, and 1 character if they are different. Changing the length is impossible because letters cannot be added or removed.
Since the lists are unordered, we only need to match the multiset of sizes from the previous year (a) with the multiset from the current year (b). Because lengths must be preserved, we can only match strings of the same length.
For each size type, let freq_a[t] and freq_b[t] be the number of occurrences. The maximum number of strings that can stay unchanged is the sum over all types of min(freq_a[t], freq_b[t]). Every other string must be changed exactly once. Therefore the minimal number of seconds is
[
n - \sum_{t} \min(\text{freq}_a[t], \text{freq}b[t]) = \frac{1}{2} \sum{t} |\text{freq}_a[t] - \text{freq}_b[t]|.
]
We compute the frequencies for both lists and then apply the formula.
import sys
from collections import Counter
def main():
it = iter(sys.stdin.read().strip().splitlines())
n = int(next(it))
cnt_a = Counter()
for _ in range(n):
s = next(it)
cnt_a[s] += 1
cnt_b = Counter()
for _ in range(n):
s = next(it)
cnt_b[s] += 1
total_diff = 0
all_keys = set(cnt_a.keys()) | set(cnt_b.keys())
for k in all_keys:
total_diff += abs(cnt_a.get(k, 0) - cnt_b.get(k, 0))
ans = total_diff // 2
print(ans)
if __name__ == "__main__":
main()