We need to parse problem: We have two strings s and t. We can apply operations:
-
Using suffix automaton: remove any single character from the string. (Maybe repeatedly? It says "By applying it once to a string, he can remove from this string any single character." So each application of suffix automaton operation removes one character. They can use it multiple times.)
-
Using suffix array: swap any two characters of the string. (By applying it once, you can swap any two characters. So each operation swaps two positions.)
We can use these operations unlimited times in any order. We want to know if it's possible to transform s into t.
We need to output:
- "need tree" if impossible even with both.
- "automaton" if possible using only removal operations (suffix automaton) i.e., we don't need swaps.
- "array" if possible using only swaps (suffix array) i.e., we don't need removals.
- "both" if we need both operations.
It's guaranteed that if only swaps suffice, then removals alone cannot, and vice versa? The statement: "It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton." So the output categories are mutually exclusive. Actually both can be used, but they guarantee that the cases are distinct: if only array works, automaton alone doesn't; if only automaton works, array alone doesn't. So we don't have ambiguity.
Goal: determine the minimal set of operations needed.
Observation: The operations: removals (delete any characters) and swaps (permute characters). So essentially we can delete characters from s, and we can reorder the remaining characters arbitrarily via swaps. But note: swaps only change order; they don't change multiset. So after deletions, the multiset of remaining characters must equal multiset of t. And we need to be able to reorder via swaps to get exactly t. Since swaps allow any permutation (they generate the full symmetric group), we can reorder arbitrarily. So the condition for possibility using both is: after deleting some set of characters from s, the multiset of remaining characters equals multiset of t. Equivalently, t must be a subsequence? Not exactly: order doesn't matter due to swaps. So we need to be able to select a multiset from s that matches t's multiset. That means for each character, the count in s must be at least count in t. If for some character, count_s < count_t, then impossible. So condition for possibility using both (or either) is: for all letters 'a'-'z', freq_s[c] >= freq_t[c]. If that fails, output "need tree".
Now, if that condition holds, we need to determine if only automaton suffices, only array suffices, or both needed.
- Only automaton: using only deletions. That means we can delete characters from s to get t, but we cannot reorder. So t must be a subsequence of s. Because we can only delete characters, order preserved. So if t is subsequence of s, we can just delete the characters not needed. So "automaton" if t is subsequence of s AND freq_s >= freq_t (but the latter is automatically satisfied if subsequence? Actually if t is subsequence, then all characters of t appear in order, so counts are definitely <= s counts, but there could be extra needed characters? Actually if it's subsequence, then all characters of t appear in s in order, so each character in t appears at least once, but could be less than freq_t? Wait, subsequence means the exact sequence appears as a subsequence; that implies the total counts of each character in t are less than or equal to those in s because t's characters are taken from s. So condition holds automatically. So we just check if t is subsequence of s. If yes, output "automaton". However, note the guarantee: if only array works, automaton alone does not; so if t is subsequence, then automaton works, but could array also work? Possibly, but we output the minimal: automaton is better? The problem says: Print "automaton" if you need only the suffix automaton to solve the problem. If both would work, we need to output the simplest? Actually the problem states: "Print "automaton" (without the quotes) if you need only the suffix automaton to solve the problem." So if it's possible with only automaton, even if it's also possible with only array? But they guarantee that these cases are mutually exclusive: "It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton." So if both could individually work, that would be ambiguous. They guarantee that cannot happen. So we can check each condition independently. So if t is subsequence, then it's possible with only automaton. However, is there any case where t is not subsequence but only array works? That is, when t is a permutation of a subset? Actually array only (swaps) requires no deletions, i.e., we must use all characters of s? Wait, operation: suffix array allows swapping any two characters. But it does not allow deletion. So using only suffix array, we cannot remove characters. So to transform s to t using only swaps, we must have s and t of the same length? Actually swapping only changes order, not length. So if we only use swaps, we cannot change the number of characters. So the length of s must equal length of t. And also the multiset of characters must be exactly same (because swaps only permute). So condition for only array is: |s| == |t| and freq_s == freq_t (i.e., they are anagrams) and also we need to be able to reorder to exactly t. Since any permutation is possible with swaps, the condition is just that s and t are anagrams. However, note: we might also need that t is a permutation of s; that is exactly freq_s == freq_t and |s|=|t|. So if that holds, we can transform s to t by swapping. So "array" if freq_s == freq_t and lengths equal. But is there any nuance? Could we also need to check if we can achieve t exactly with swaps? Since we can swap any two characters arbitrarily, yes we can reorder to any permutation. So condition: s and t are anagrams.
But wait: The problem says: "Print "array" (without the quotes) if you need only the suffix array to solve the problem." That means it is possible with only swaps, and not possible with only automaton. So if both lengths equal and anagrams, then only array works? Possibly also automaton could work if t is subsequence of s? But if lengths equal and s and t are anagrams, and s != t, then t being subsequence of s? Since lengths equal, if t is subsequence of s, that would mean s == t (because a subsequence of same length must be exactly the string). So unless s==t, automaton would not work. If s==t, then both automaton (do nothing? But we could do zero deletions? Actually we don't need to do any operations; but the problem says "if you need only the suffix automaton to solve the problem." Does zero operations count as using automaton? Possibly any solution exists, but we want minimal? The guarantee ensures that if s==t, then both automaton and array would work? Actually if s==t, we could trivially do nothing, which uses no operations, but it's ambiguous. However, the guarantee says such ambiguous case won't occur? Let's read: "It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton." So they guarantee that if array-only solution exists, automaton-only does not. So if s==t, then both exist? Actually both automaton (by doing nothing) and array (by doing nothing) exist. But they say it's guaranteed that won't happen? They don't explicitly say that s and t are different? In the input description: "Words s and t are different." Yes, first line: "The first line contains a non-empty word s. The second line contains a non-empty word t. Words s and t are different." So s != t. So equal strings are not allowed. Good. So we don't need to consider s==t.
Thus, if |s| == |t| and freq_s == freq_t, then array-only works, and automaton-only cannot because then s and t are anagrams but different, so t is not subsequence unless s==t, which is excluded. So we can output "array".
Now, if freq_s[c] >= freq_t[c] for all c, but neither t is subsequence (so automaton fails) nor lengths equal with equal freq (so array fails), then we need both operations. That corresponds to case where we need to delete some characters and also reorder. So output "both". However, also check the possibility that we might need both even if one condition holds? But the guarantee says if automaton works alone, array alone doesn't, so we output automaton. So classification is straightforward.
But careful: Could there be a scenario where freq_s >= freq_t and t is subsequence, but we also need to delete some characters? That's exactly automaton. Yes.
Could there be a scenario where t is subsequence but also lengths equal? That would force s==t, which is excluded. So fine.
So algorithm:
- Compute frequency arrays for s and t.
- If for any c, freq_s[c] < freq_t[c], output "need tree".
- Else:
- Check if t is subsequence of s.
- If yes: output "automaton".
- Else if |s| == |t|: output "array". (Because if lengths equal and freq_s >= freq_t, but since sum of freqs equals length, freq_s >= freq_t and lengths equal implies freq_s == freq_t. So we can check simply if |s| == |t| and freq_s >= freq_t, which implies equality. So we can output "array". Actually we need to ensure that array-only works: that means we can achieve t by swapping only. The condition is exactly that s and t are anagrams. Since we already know freq_s >= freq_t and lengths equal, it forces equality. So "array".)
- Else: output "both".
But wait: Could there be a case where |s| > |t| but t is not subsequence, yet freq_s >= freq_t, so both needed? Yes.
But what about the case where |s| == |t| but freq_s >= freq_t and t is not subsequence? That would be array-only because freq_s==freq_t. But we must also consider if t is not subsequence? That's fine. So output "array". That matches guarantee.
But double-check the guarantee: "It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton." So for array-only, automaton-only must be impossible. That holds because if lengths equal and anagrams, t is not subsequence unless s==t. So fine.
Now we need to confirm edge cases: Suppose s = "ab", t = "ba". Then freq_s = {a:1,b:1}, freq_t = {a:1,b:1}, lengths equal, so output "array". Is automaton possible? No, because to get "ba" from "ab" by deletions only you'd have to delete both and reorder? Deletions only preserve order, can't reorder, so impossible. So correct.
Suppose s = "aba", t = "ba". freq_s >= freq_t (a:2,b:1 vs a:1,b:1). t is subsequence? "ba": s = "aba": can we get b then a? positions: b at index1 (0-based) and a at index2, yes subsequence. So automaton works. Output "automaton". Is array-only possible? Lengths not equal, so no.
Suppose s = "abb", t = "bab". freq_s: a1,b2; t: b2,a1; lengths equal, anagrams, so array works. But t is subsequence? To check: "bab" from "abb": we need b,a,b. s = a b b. subsequence: first b at index1, then a? but a is before that index? Actually after index1, the next characters are index2: b. a is at index0, which is before index1, can't use because order must be increasing. So no subsequence. So automaton fails, array works. Good.
Suppose s = "ab", t = "a". freq_s >= freq_t, t is subsequence, so automaton. But length not equal, so array not possible. Good.
Suppose s = "ab", t = "b". automaton.
Suppose s = "aab", t = "ba". freq_s: a2,b1; t: b1,a1. lengths differ. t is subsequence? Check "ba": b then a. s = a a b. b appears at index2, after that there is no a, so not subsequence. So both needed? We can delete one 'a' to get "ab" and then swap? Wait we need to get "ba". Process: start "aab". Delete first 'a' (using automaton) => "ab". Then swap => "ba". So both needed. So output "both". Our algorithm: freq_s >= freq_t yes, check subsequence? false, check |s|==|t|? false, so "both". Good.
Now what about the scenario where t is subsequence but we also need to delete and reorder? Since automaton suffices, we output automaton.
Now confirm that condition for automaton is strictly subsequence? Could there be any nuance with automaton operation: "By applying it once to a string, he can remove from this string any single character." So each operation removes exactly one character. So multiple deletions are allowed. So indeed we can remove any set of characters (order preserved). So subsequence condition.
One more nuance: Could there be a scenario where t is subsequence but we also need to reorder because maybe we need to delete some and then reorder? If t is subsequence, we can just delete others and get exactly t without reordering. So automaton alone works.
Thus algorithm is solid.
Now we must implement.
Complexities: length up to 100, trivial.
Implementation Steps:
- Read strings s and t.
- Compute freq_s[26], freq_t[26].
- For c in 0..25: if freq_s[c] < freq_t[c] -> print "need tree" and return.
- Else:
- Check if t is subsequence of s:
i = 0, j = 0;
while i < len(s) and j < len(t):
if s[i] == t[j]: j++
i++
if j == len(t): is_subseq = True else False.
- If is_subseq: print "automaton"
- Else if len(s) == len(t): print "array" (since anagrams guaranteed)
- Else: print "both"
Now, one more check: In case where lengths are equal but anagrams? Our condition: if is_subseq is false but lengths equal, then print "array". But suppose freq_s >= freq_t, lengths equal, but is_subseq is true? That would imply s == t as argued. Because if lengths equal and t is subsequence of s, then s must equal t. Proof: Let |s| = |t|. If t is subsequence of s, then we can match all characters in order, but since lengths are equal, there can be no extra characters; so the subsequence uses every character of s, meaning s[i] == t[matched indices] for all i. So s==t. Since s != t, that can't happen. So safe.
Now also check the case where lengths equal but freq_s >= freq_t and not anagram? Actually if lengths equal and freq_s[c] >= freq_t[c] for all c, then sum freq_s = sum freq_t = length, so all inequalities must be equalities. So indeed anagrams.
Thus algorithm correct.
But note: The problem statement also says: "It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton." So our classification will respect that.
Now test on given examples:
-
s="automaton", t="tomat". freq_s: a2,u1,t2,o2,m1,n1? Actually "automaton": a u t o m a t o n. counts: a:2, t:2, o:2, u:1, m:1, n:1. t="tomat": t:2? t o m a t: t:2, o:1, m:1, a:1. So all freq_s >= freq_t. Check subsequence: "tomat" in "automaton"? Let's see: t (index2), o (index3), m (index4), a (index5), t (index6). Yes subsequence. So output "automaton". Matches.
-
s="array", t="arary". freq_s: a2,r2,y1. t: a2,r2,y1 (same). lengths equal 5. Check subsequence? "arary" from "array": s= a r r a y. t= a r a r y. Check: a at 0, r at1, next needed a: after index1, we have r at2, then a at3 -> okay we can take a at index3, then r? after index3, we have y at4, no r. So not subsequence. So output "array". Matches.
-
s="both", t="hot". freq_s: b1,o1,t1,h1. t: h1,o1,t1. All good. subsequence? "hot" in "both"? b o t h? Actually s = b o t h. t = h o t? Wait t = "hot" -> h o t? Actually "hot" is h,o,t. But order: h,o,t. In s: b,o,t,h. To get h first, we need h before others. But h appears last. So not subsequence. length? s len=4, t len=3, not equal. So output "both". Matches.
-
s="need", t="tree". freq_s: n1,e2,d1; t: t1,r1,e2. freq_s[t]? t not in s: 0 < 1, so impossible -> "need tree". Matches.
All good.
Edge Cases: s longer, t longer? Actually t cannot be longer than s if freq_s >= freq_t? Because total count sum of t must be <= sum of s. So length of t <= length of s. So fine.
Thus answer.
Now we'll implement in Python.
But wait: Could there be a scenario where t is subsequence but we need to use both? No.
Now output exactly as needed.
We'll code:
def solve():
import sys
s = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
# frequency
from collections import Counter
cnt_s = Counter(s)
cnt_t = Counter(t)
for ch, cnt in cnt_t.items():
if cnt_s[ch] < cnt:
print("need tree")
return
# check subsequence
i = j = 0
while i < len(s) and j < len(t):
if s[i] == t[j]:
j += 1
i += 1
if j == len(t):
print("automaton")
return
if len(s) == len(t):
print("array")
else:
print("both")
Testing with examples.
Thus final answer.
But wait: The problem says "suffix automaton" operation can remove any single character. So deletions are allowed. That's what we used.
Now confirm: In some cases, maybe we can also use automaton to delete characters and then array to swap? That's both.
Thus done.
We'll produce final solution. ```python
import sys
from collections import Counter
def solve() -> None:
s = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
cnt_s = Counter(s)
cnt_t = Counter(t)
# Check if enough characters exist
for ch, need in cnt_t.items():
if cnt_s[ch] < need:
print("need tree")
return
# Check if t is a subsequence of s (only deletions needed)
i = j = 0
while i < len(s) and j < len(t):
if s[i] == t[j]:
j += 1
i += 1
if j == len(t):
print("automaton")
return
# If lengths are equal, we can reorder (only swaps needed)
if len(s) == len(t):
print("array")
else:
print("both")
if name == "main":
solve()