루미 알고리즘

#1590플래티넘Protein Synthesis

PO 2024 온라인 예선코딩출제 Joshua Andersson영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

메모리 제한 1024 MB

Problem

Latbo is well known to be the laziest city in Sweden. Björn has had the goal of being the laziest in town since he was little. Yesterday, he made it all the way to the final in this year's laziness competition. He was fully prepared to win but was completely crushed when his opponent was too lazy to even show up.

This was the last straw for Björn, and he has now decided to use his medical knowledge to develop a cure for laziness. To do this, he needs to synthesize the magical protein T.

Since he is too lazy to determine how to do this himself, he is now asking for your help in finding the best way to construct protein T. All proteins, including protein T, can be represented as a string of letters from a-z. Björn knows how to produce M types of proteins. It takes one day to create one of these M proteins, regardless of how long or short they are. He also has tools to combine two proteins. For example, he can take the proteins abc and ba and combine them into either abcba or baabc.

Since Björn wants to produce protein T as quickly as possible, he wants your help in figuring out how many days it will take to produce it if he spends his time optimally. The time it takes to combine proteins is negligible, so you only need to consider the time it takes to produce the proteins.

Input

The first line of the input contains the integer N (1 ≤ N ≤ 10^5), the length of protein T.

The next line contains a string of length N, representing protein T.

The next line contains M (1 ≤ M ≤ 10^6), the number of proteins Björn can manufacture.

Afterwards, M lines follow, each containing a string. This string represents a protein he can produce. It is guaranteed that the total length of the M strings does not exceed 10^6.

All strings in the input consist of letters a-z. It is guaranteed that Björn can synthesize T by only using the M proteins Björn can manufacture.

Output

Print an integer: the number of days it takes for Björn to create protein T if he spends his time optimally.

Scoring

Your solution will be tested on a set of test groups. To earn points for a group, you must pass all test cases in that group.

GroupPointsConstraints
110N ≤ 20, M ≤ 5
220N, M ≤ 100
330M ≤ 100
440No additional constraints.

Explanation of sample 1

By creating the protein abc twice, he can combine them to create the protein abcabc.

예제

예제 1

입력
6
abcabc
5
a
b
c
abc
abca
출력
2

예제 2

입력
5
aaaaa
4
aaaa
aa
aa
aaa
출력
2

출처 표기

출처: Programmeringsolympiaden 2024년 온라인 예선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.