문제
For each requested pattern, decide whether it appears as a contiguous substring anywhere in the shared text. Patterns may overlap and may repeat. Return results in the original query order. The limits require an approach that can handle many patterns efficiently.
Implement find_many_patterns(text, patterns).
Input
text: The text searched by every query.
patterns: The patterns, in query order.
Output
Return YES for each occurring pattern and NO for each absent one.
제약 조건
1…100,000 text characters. 1…100,000 nonempty patterns, with total length at most 500,000. All characters are lowercase a–z.
예시
입력:
text = "bananas"
patterns = ["ana", "apple", "nas"]
예상 출력:
expected = ["YES", "NO", "YES"]
입력:
std::string text = "bananas";
std::vector<std::string> patterns = {"ana", "apple", "nas"};
예상 출력:
std::vector<std::string> expected = {"YES", "NO", "YES"};
입력:
string text = "bananas";
string[] patterns = new string[] {"ana", "apple", "nas"};
예상 출력:
string[] expected = new string[] {"YES", "NO", "YES"};
설명
Ana appears starting at position 2 and nas at position 5 (positions start at 1 here). Apple never appears.
입력:
text = "aaaa"
patterns = ["aa", "aaaaa", "aa"]
예상 출력:
expected = ["YES", "NO", "YES"]
입력:
std::string text = "aaaa";
std::vector<std::string> patterns = {"aa", "aaaaa", "aa"};
예상 출력:
std::vector<std::string> expected = {"YES", "NO", "YES"};
입력:
string text = "aaaa";
string[] patterns = new string[] {"aa", "aaaaa", "aa"};
예상 출력:
string[] expected = new string[] {"YES", "NO", "YES"};
설명
Aa occurs despite overlapping matches. Five a characters cannot fit in a four-character text. The repeated aa query receives the same answer.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
Many patterns can share work, so avoid searching the text separately for each query.
힌트 2
Build one structure that stores all patterns and scan the text once, advancing matches as characters arrive.
힌트 3
Be careful to report repeated patterns in query order, and let overlapping matches count independently.
예상 복잡도
O(text length + total pattern length) time, O(total pattern length) extra space
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.