← 返回 bytedance 的题目列表Most Common Suffix / Longest Common Suffix Queries
类型:online_judge
Problem: Longest Common Suffix Queries
You are given an array of strings wordsContainer and an array of query strings wordsQuery. For each query string q, find a string w in wordsContainer such that w and q have the longest common suffix, and return the index of w in wordsContainer.
If multiple strings have the same longest common suffix length:
Return the shorter string;
If there is still a tie, return the smaller index.
If there is no non-empty common suffix, still return the shortest string in wordsContainer, breaking ties by the smallest index.
Input Format
n
word_0 word_1 ... word_{n-1}
q
query_0 query_1 ... query_{q-1}
Output Format
Print q integers separated by spaces, where each integer is the answer index for the corresponding query.
Constraints
1 <= n, q <= 10^4
1 <= len(wordsContainer[i]), len(wordsQuery[i]) <= 5000
The total length of all strings is at most 5 * 10^5
All strings contain only lowercase English letters.
Example
Input:
3
abcd bcd xbcd
3
cd zzcd abcd
Output:
1 1 0
Explanation:
cd has common suffix length 2 with all three words. The shorter word bcd is chosen, so the answer is index 1.
zzcd is similar and chooses bcd.
abcd fully matches abcd, so the answer is index 0.
Example
Input
3
abcd bcd xbcd
3
cd zzcd abcd
Output
1 1 0