KMOP

시간 제한0.5초메모리 제한1024 MB

요약
각 단어에서 길이 1에서 3까지의 접두사를 순서대로 이어 붙여, 자음이 세 개 연속 나오지 않으면서 전체 길이가 최소인 약어를 찾는다.
난이도

보통10점 중 6점

유형
동적 계획법, 문자열, 그리디
정답자
아직 제출이 없습니다

문제

You probably know the KMP algorithm. You may also know that “KMP” is an acronym that stands for “Knuth Morris Pratt”, who jointly published the algorithm in 1977. How do you pronounce “KMP”? Of course, you can just say “Knuth Morris Pratt”, but what about pronouncing the acronym itself? Since “KMP” is not a pronounceable word, you are forced to say the letters one by one. In this problem we are interested in pronounceable acronyms.

We need a few definitions to formalize the requirement. A phrase is a list of words and a word is a sequence of letters. Each letter is either a vowel or a consonant. Deciding whether a letter is a vowel or a consonant depends on the language and other elements. For simplicity, we say that the six letters “A”, “E”, “I”, “O”, “U” and “Y” are vowels, while all the rest are consonants. Although it is debatable whether a given word is pronounceable, we say that a word is pronounceable when it does not contain more than two contiguous consonants. For instance, “LEMPEL” is a pronounceable word, while “DIJKSTRA” is not.

Given a phrase composed of NN words, an acronym for the phrase is the concatenation of N prefixes, one prefix for each word, in the order they appear in the phrase. Each prefix must have at least one and at most three letters. Your task is to determine the minimum length a pronounceable acronym can have.

As an example with N=3N = 3 consider the phrase “KNUTH MORRIS PRATT”. There are 2727 possible acronyms for this phrase, such as “KMP”, “KMPR”, “KMPRA”, “KMOP”, “KMOPR” and “KNUMORPRA”, among others. Some of these acronyms are pronounceable (“KMOP” and “KMOPR”), while some others not (“KMP”, “KMPR”, “KMPRA” and “KNUMORPRA”). Since the only three-letter acronym “KMP” is not pronounceable, it follows that “KMOP” is a minimum-length pronounceable acronym for the phrase.

입력

The first line contains a positive integer NN indicating the number of words in the phrase.

Each of the next NN lines contains a non-empty string made of uppercase letters representing a word in the phrase. Words are given in the order they appear in the phrase. The sum of the lengths of all the strings is at most 10610^6.

출력

Output a single line with an integer indicating the minimum length a pronounceable acronym can have, or the character “*” (asterisk) if no pronounceable acronym exists for the phrase.

예제5

  1. 예제 1

    입력
    3
    KNUTH
    MORRIS
    PRATT
    
    예상 출력
    4
    
  2. 예제 2

    입력
    3
    KNUTH
    M
    PRATT
    
    예상 출력
    5
    
  3. 예제 3

    입력
    3
    K
    M
    P
    
    예상 출력
    *
    
  4. 예제 4

    입력
    2
    K
    M
    
    예상 출력
    2
    
  5. 예제 5

    입력
    4
    YOU
    SHOULD
    BE
    DANCING
    
    예상 출력
    5