Enigmatic Number

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

요약
1000 미만의 주어진 수 집합에서 각 수를 최대 한 번씩만 사용해 십진수 N을 가장 적은 개수의 조각으로 이어 붙이는 분할을 찾는다.
난이도

보통10점 중 7점

유형
동적 계획법, 문자열, 완전 탐색, 비트 연산
정답자
아직 제출이 없습니다

문제

John is a junior researcher in the Research Institute for the Meaning of Life. Recently, he discovered that the decimal number NN contains knowledge that can shed light on some of the issues of the human life. However, John is selfish person and he does not want to share his discovery. So he decided to remember this number and to eat all records associated with it.

Unfortunately, John is a bit distracted and forgetful, so he decided to use the following way to remember the number NN. There is a set of KK numbers connected with the life of John, which he remembers very well, even better than the date of his birth. All these numbers contain no more than three digits.

John is trying to represent the number NN as a concatenation of numbers from this set (e.g., a concatenation of the numbers 11 and 22 can give 1212 or 2121 depending on the order). No number can be used twice, on the other hand it is not obligatory to use all numbers. John wants to find (for easier remembering) a representation, containing as few numbers as possible.

Write a program that will find such a representation.

입력

First line of the input file contains an integer number NN (1≤N<10451 \le N < 10^{45}). Second line contains an integer number KK --- size of the memorable numbers set (1≤K≤10001 \le K \le 1000). Third line contains KK memorable numbers (0≤a_i<10000 \le a\_i < 1000). All numbers in the set are different and do not have extra leading zeroes.

출력

In the first line of the output file output an integer number MM --- size of the desired partition. In the following MM lines output the numbers forming the partition (in the order in which they need to be concatenated to obtain the number NN).

If there exist multiple partitions satisfying the criteria described, output any of them. It is guaranteed that at least one desired partition of NN exists.

예제2

  1. 예제 1

    입력
    123
    4
    1 3 12 23
    
    예상 출력
    2
    12
    3
    
  2. 예제 2

    입력
    123
    4
    1 2 3 123
    
    예상 출력
    1
    123